From the course: Learning ASP.NET

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Consume a web API from a client

Consume a web API from a client

From the course: Learning ASP.NET

Start my 1-month free trial

Consume a web API from a client

- [Instructor] Our MVC client application needs to consume the Roux Academy Web API in order to get some student data. So how does ASP.NET bypass the same origin policy? It allows us to enable cross-origin resource sharing or CORS for short. Right-click the RouxAcademyWebApi project and select Manage NuGet Packages. In the Browse tab, search for the package Microsoft.AspNet.WebApi.Cors and install it. Under the RouxAcademyWebApi project, open the App_Start folder and the WebApiConfig file. I'll add this code to the top of the register method. We'll use the HttpConfiguration object and call EnableCors. What this does is enable CORS support in our Web API. Next, open the student Web API controller, and make sure you add the namespace for CORS. For the origins parameter, use the URL for the MVC application. This allows cross-origin requests from our MVC site, while still preventing other cross-domain requests. For headers, type in an asterisk to allow all, and do the same for methods…

Contents