CORS for static HTML in vNext

1.3k Views Asked by At

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?

2

There are 2 best solutions below

3
On BEST ANSWER

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();
    }
0
On

You need to allow the server to accept CORS.

app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

Client(browser) must know that server accepts the CORS because browser checks for the Allow Cors in the response of server, if it's allowed or not and then it allows displaying data even if it's static content like plain HTML.