
When it's all said and done you now should get the appropriate CORS headers with your response: HTTP/1.1 200 OKĬontent-Type: application/json charset=utf-8Īccess-Control-Allow-Origin: Content-Length: 2851 Public class AlbumViewerApiController : Controller Or you can apply the policy to individual controllers: Make sure you declare the CORS functionality before MVC so the middleware fires before the MVC pipeline gets control and terminates the request. UseCors() has to be called before UseMvc() IMPORTANT: Make sure UseCors() is called BEFORE this global policy - assign here or on each controller You can apply the policy globally to every request in the application by call app.useCors() in the Configure() method of Startup: public void Configure(IApplicationBuilder app) Once the policy has been defined it can be applied. There are other ways to do essentially the same thing by explicitly adding a policy builder in the configuration step but to me this seems cleanest - define one or more policies up front and then apply it. The AddCors() call above adds the CORS features to ASP.NET and creates a custom policy that can be reused in the application by name. Add service and create Policy with options To do this start with registering CORS functionality in ConfigureServices() of Startup.cs: public void ConfigureServices(IServiceCollection services)

When browsers make cross domain calls using XHR, they request CORS headers to decide whether the target server allows access to the source domain.

It's good to be king, huh? (especially a king with no clothes since the protocol does next to nothing to prevent malicious attacks but that's a story for another post) I allow cross domain calls from the domains I specify CORS Setup in ASP.NET CoreĬORS is a server based mechanism that essentially lets a server say: Alas those calls failed and upon closer inspection it was due to the fact that the CORS headers weren't getting sent. Angular 2.0's default working environment runs a development server off a seperate port which is effectively a seperate domain and all calls back to the main ASP.NET site for the API calls effectively are cross domain calls. Last night I was working on updating my ASP.NET Core AlbumViewer sample application to Angular 2.0 and in the process ran into CORS problems.
