In modern web applications developers mostly use any client side frameworks like AngularJS, EmberJS, Knockout etc. and in order to make server-side request they either call a Web API or a web service through XMLHttpRequest object and perform CRUD operations.
In internet explorer it ignores the port no. so if both are on localhost or same base address it will call it without any issue. But this does not solve the case. In order to develop a service or a Web Api that has to be accessed by any client we need to handle it properly.
In this post, I will show how simply with few steps we can enable Cross Origin Resource sharing in ASP.NET Web Api.
- First of all in your Web API project add a NuGet Package “Microsoft.AspNet.WebApi.Cors” through NuGet Package Manager
- Then in the WebApiConfig.cs class add an entry to enable cors.
- And finally add the EnableCors attribute on Controller and specify the allowed origins, headers and methods
- You can set origin, header and methods as below.
Hope this helps!