public class MoviesService
{
const string baseUrl = "http://localhost:2100/api/movies1/";
public Task<IEnumerable<Movie>> GetAllAsync()
{
var client = new HttpClient();
var task = client.GetStringAsync(baseUrl);
return task.ContinueWith<IEnumerable<Movie>>(innerTask =>
{
var json = innerTask.Result;
return JsonConvert.DeserializeObject<Movie[]>(json);
});
}
}
RajeshKalis
Member
27 Points
54 Posts
Call Web API controller actions from C#.net client with HttpClient object
Oct 31, 2012 01:36 PM|LINK
Hi,
I need some help on calling web api controller actions from C#.net client application.
I have created a web api controller class LibraryController.cs and added the following code in this class.
[HttpGet] public System.Web.Mvc.JsonResult GetAllBooks() { System.Web.Mvc.JsonResult jsonResult = new System.Web.Mvc.JsonResult(); jsonResult.Data = <List<Book> object>; return jsonResult; }I can call this action from browser and through javascript directly with the url http://localhost:1982/api/Library/GetAllBooks
But, how can I call this from C#.net code with HttpClient object? I need to call this action from both jquery ajax and C#.net.
K. Rajesh.
BrockAllen
All-Star
28072 Points
4996 Posts
MVP
Re: Call Web API controller actions from C#.net client with HttpClient object
Oct 31, 2012 02:21 PM|LINK
http://blogs.msdn.com/b/webdev/archive/2012/08/26/asp-net-web-api-and-httpclient-samples.aspx
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
RajeshKalis
Member
27 Points
54 Posts
Re: Call Web API controller actions from C#.net client with HttpClient object
Nov 02, 2012 06:50 AM|LINK
Hi Allen,
Thank you for your reply, but I did not find any solution from those samples.
K. Rajesh.
dhanekula
Member
101 Points
37 Posts
Re: Call Web API controller actions from C#.net client with HttpClient object
Nov 06, 2012 05:16 PM|LINK
Hi Rajesh,
http://www.asp.net/web-api/overview/web-api-clients
Please go throught these examples. If you are working in MVC4 i will recommend use asp.net WebAPI .
RajeshKalis
Member
27 Points
54 Posts
Re: Call Web API controller actions from C#.net client with HttpClient object
Nov 08, 2012 06:43 AM|LINK
Hi Dhanekula,
Sorry to say, these samples did not solved my problem.
K. Rajesh.
BrockAllen
All-Star
28072 Points
4996 Posts
MVP
Re: Call Web API controller actions from C#.net client with HttpClient object
Nov 08, 2012 02:03 PM|LINK
Something like this:
public class MoviesService { const string baseUrl = "http://localhost:2100/api/movies1/"; public Task<IEnumerable<Movie>> GetAllAsync() { var client = new HttpClient(); var task = client.GetStringAsync(baseUrl); return task.ContinueWith<IEnumerable<Movie>>(innerTask => { var json = innerTask.Result; return JsonConvert.DeserializeObject<Movie[]>(json); }); } }DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
RajeshKalis
Member
27 Points
54 Posts
Re: Call Web API controller actions from C#.net client with HttpClient object
Nov 20, 2012 12:19 PM|LINK
Hi Allen,
I have created a new thread with clear example for my requirement.
http://forums.asp.net/p/1860015/5215674.aspx/1?p=True&t=634889961854469845
Please give me your suggestions.
K. Rajesh.