0

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.

Community
  • 1
  • 1
Gertsen
  • 1,078
  • 20
  • 36

1 Answers1

2

Add in this

using Microsoft.AspNet.Builder;

and make sure you have the package in your project.json file

Microsoft.AspNet.Cors.Core

This is an image of the extensions

enter image description here

Adam
  • 16,089
  • 6
  • 66
  • 109
  • My project is using Beta4 so unless they have gone around changing namespaces again (which they have been doing), it should be right. – Adam Aug 06 '15 at 09:00
  • Odd, `Microsoft.AspNet.Cors.Core` (1.0.0-beta6) didn't solve the problem, but `Microsoft.AspNet.Cors` (1.0.0-beta5) did. – Gertsen Aug 06 '15 at 09:02
  • 1
    Looks like some namespace changes. Just make sure the DNX and all packages are the same beta version. For example beta4 doesn't work well with beta6 etc. – Adam Aug 06 '15 at 09:06
  • All my other beta-packages are beta6, but `Microsoft.AspNet.Cors` is only available in beta5 :-/ - It is available in a final version `5.2.3` but if I use that one, the problem comes back, so for now `beta5` seems like the best bet, even though I don't like mixing beta versions.- I tried adding both the final `Microsoft.AspNet.Cors` and the beta6 `Microsoft.AspNet.Cors.Core` but that didn't solve the problem either. So far only `Microsoft.AspNet.Cors` beta5 without the .Core seems to work. – Gertsen Aug 06 '15 at 09:11
  • Odd, I just tried changing `1.0.0-beta5` to `1.0.0-beta6` for `Microsoft.AspNet.Cors` and it worked. Now I have maching beta versions, and no error messages. Guess IntelliSense isn't always right ;-) – Gertsen Aug 06 '15 at 09:17