Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CORS for static HTML in vNext

I have an MVC6 site running in Visual Studio 2015 RC

I've got some static HTML files I want to serve to another website. I want to add CORS support (without having to add a controller and add CORS that way).

Does anyone know how to do this please?

like image 281
Andy Clarke Avatar asked Jun 18 '15 11:06

Andy Clarke


1 Answers

In Startup.cs

Configure the policy in ConfigureServices ...

    public void ConfigureServices(IServiceCollection services)
    {
            options.AddPolicy("AllowEverything", builder =>
            {
                builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().AllowCredentials();
            });
    }

Then in Configure set the app to use the policy, then set UseStaticFiles ...

Ensure that UseStaticFiles() comes after UseCors - at least in the version I'm using (installed with Visual Studio 2015 RC) it needs to come after UseCors()

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseCors("AllowEverything");
        app.UseStaticFiles();
    }
like image 160
Andy Clarke Avatar answered Oct 22 '22 20:10

Andy Clarke