You can user HttpClient library to call the web api from you MVC controller.
Below is a bit code as example.
Use System.Net.Http.Formatting; and System.Net.Http; and System.Threading.Tasks;
Also use async for the action method like : public async Task<ActionResult> Report() {}
Example code :
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(basewencmsurl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("x-access-token", token);
var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("ChannelId", ChannelId.Trim()));
HttpContent content = new FormUrlEncodedContent(postData);
HttpResponseMessage response = await client.PostAsync("getLocations_type1", content);
if (response.IsSuccessStatusCode)
{
LocationResult output = await response.Content.ReadAsAsync<LocationResult>();
result = RenderRazorViewToString("~/Views/WebCMS/_ChannelLocations.cshtml", output.result);
If your website is hosting together with Web API controller then you can call Web API as ordinary class method to avoid any delays related to HTTP request ,you can also just use a common business layer and on Client side to call the Web API directly .
If it's hosted separately, then use
HttpClient to consume Web API from your controller ,you could refer links below for details and demo:
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
You could refer to link below for how to use Web API and JQuery to GET or POST Data in mvc, you could use it in jquery at client side and MVC controller :
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
you can serialize your return object to json format before returning, also change the return type to JObject or whatevever object you want instead of string
Member
11 Points
70 Posts
Call Web API Controller from MVC action by passing multiple parameters
Sep 10, 2015 02:49 AM|santoshhegde|LINK
Hi,
I have MVC controller like below. I need to call Web API controller from controller by passing multiple parameters.
public ActionResult Report()
{
// need to call Web API from here by passing below parameter to Web API
// Parameters need to be passed to Wev API controller: int param1,string param2,DataTable param3, string param4, string param5
return View();
}
My Web API method is like below:
public class TestController : ApiController {
[HttpGet]
public string GetJSONData(HttpRequestMessage request)
{
// here I need to get all the parameter and pass it to GetJSONData method below
return GetJSONData(param1,param2,param3,param4,param5);
}
}
Please help me to provide some example for the above.
Member
2 Points
21 Posts
Re: Call Web API Controller from MVC action by passing multiple parameters
Sep 10, 2015 03:54 AM|Amitabha Saha|LINK
Hi,
You can user HttpClient library to call the web api from you MVC controller.
Below is a bit code as example.
Use System.Net.Http.Formatting; and System.Net.Http; and System.Threading.Tasks;
Also use async for the action method like : public async Task<ActionResult> Report() {}
Example code :
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(basewencmsurl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("x-access-token", token);
var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("ChannelId", ChannelId.Trim()));
HttpContent content = new FormUrlEncodedContent(postData);
HttpResponseMessage response = await client.PostAsync("getLocations_type1", content);
if (response.IsSuccessStatusCode)
{
LocationResult output = await response.Content.ReadAsAsync<LocationResult>();
result = RenderRazorViewToString("~/Views/WebCMS/_ChannelLocations.cshtml", output.result);
}
}
All-Star
18785 Points
3830 Posts
Microsoft
Re: Call Web API Controller from MVC action by passing multiple parameters
Sep 10, 2015 05:24 AM|Nan Yu|LINK
Hi santoshhegde,
If your website is hosting together with Web API controller then you can call Web API as ordinary class method to avoid any delays related to HTTP request ,you can also just use a common business layer and on Client side to call the Web API directly .
If it's hosted separately, then use HttpClient to consume Web API from your controller ,you could refer links below for details and demo:
http://stackoverflow.com/questions/13200381/asp-net-mvc-4-application-calling-remote-webapi .
http://forums.asp.net/t/2009532.aspx?How+do+I+call+my+Web+API+from+an+ActionMethod+
Best Regards,
Nan Yu
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
11 Points
70 Posts
Re: Call Web API Controller from MVC action by passing multiple parameters
Sep 10, 2015 07:10 AM|santoshhegde|LINK
Thanks for your reply.
I see this is POST how to call GET (I mean need to get back data from Web API).
Pass some parameter and get back the data in JSON format.
Thanks....
All-Star
18785 Points
3830 Posts
Microsoft
Re: Call Web API Controller from MVC action by passing multiple parameters
Sep 13, 2015 10:03 PM|Nan Yu|LINK
Hi santoshhegde,
You could refer to link below for how to use Web API and JQuery to GET or POST Data in mvc, you could use it in jquery at client side and MVC controller :
http://www.codeproject.com/Tips/678138/MVC-using-Web-API-and-JQuery-to-GET-or-POST-Data .
Best Regards,
Nan Yu
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
230 Points
148 Posts
Re: Call Web API Controller from MVC action by passing multiple parameters
Sep 16, 2015 05:10 AM|jmpalines|LINK
you can serialize your return object to json format before returning, also change the return type to JObject or whatevever object you want instead of string