I'm trying to enable CORS in my MVC 6 WebApi project, and I found this question, focusing on the same problem: How do you enable cross-origin requests (CORS) in ASP.NET 5 & MVC 6?
It looks exactly like what I want to do, but when I get to this part, I run into trouble.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
//Add Cors support to the service
services.AddCors();
var policy = new Microsoft.AspNet.Cors.Core.CorsPolicy();
policy.Headers.Add("*");
policy.Methods.Add("*");
policy.Origins.Add("*");
policy.SupportsCredentials = true;
services.ConfigureCors(x=>x.AddPolicy("mypolicy", policy));
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// Configure the HTTP request pipeline.
app.UseStaticFiles();
//Use the new policy globally
app.UseCors("mypolicy");
// Add MVC to the request pipeline.
app.UseMvc();
}
It (aswell as any other ressources on this subject I have found) clearly states that I should use app.UseCors("policyname")
in the Configure
method of the Startup.cs
-file.
But when I do, I get the following error:
'IApplicationBuilder' does not contain a definition for 'UseCors' and no extension method 'UseCors' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)
I know this is a brand new feature in MVC 6, maybe that explains why I havn't been able to find any official documentation on how to implement this.
Could it be something as simple as adding the correct NuGet package? I've tried several .Net NuGet packages from Microsoft related to CORS, but they didn't solve the problem, and I suspect they are related to the old version of WebApi.