Host ASP.Net web api and consume from another MVC application

I am new to ASP.Net MVC and ASP.Net web api.

I worked on and bundled web services.

But I'm not sure how to host the ASP.Net web api and consume the same from another ASP.Net MVC application. I found several examples in which people use api from the same project. In the real world, I really doubt that use will be limited to one project.

Can any body send some examples or links that can explain the same thing?

+6
source share
1 answer

Hosting an ASP.NET Web API in an MVC web application makes perfect real sense if you mainly use the API to create AJAX calls from your web client. The presence of an API and a web application in one project eliminates problems with cross-domain Ajax calls, which requires JSONP. But the ASP.NET Web API can easily support JSONP, if necessary, using custom formatting, such as JsonMediaTypeFormatter, available from WebApiContrib .

The ASP.NET Web API simplifies the development of the REST API. You use the same conventions as controller development in MVC. To create it separately from your web application, just create an MVC 4 project and create your controllers using ApiController instead of the standard controller for rendering Views. Then just deploy the application, just like any web application, to IIS (you can also run ASP.NET web APIs yourself in a bit of a Windows service). Here is an example on the official ASP.NET Web API website that calls the REST API developed by the ASP.NET Web API from a console application . There are many more examples on this site.

+5
source

Source: https://habr.com/ru/post/922365/


All Articles