How do you enable cross-origin requests (CORS) in ASP.NET Core MVC

后端 未结 6 1803
广开言路
广开言路 2020-12-05 04:34

I\'d like to enable CORS on an API built with ASP.NET Core MVC, but all the current documents refer to earlier versions of that framework.

6条回答
  •  鱼传尺愫
    2020-12-05 05:08

    In the most recent RC2 of ASP.NET Core.

    The NuGet packages are

    "Microsoft.AspNetCore.Owin": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Cors": "1.0.0-rc2-final",
    

    In Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddCors();
        services.AddMvc();
    }
    
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();
    
        app.UseCors(builder =>  builder
        .AllowAnyOrigin());
        app.UseMvc();
    }
    

提交回复
热议问题