asp.net核心CORS没有像我期望的那样工作

时间:2018-03-14 21:26:20

标签: asp.net-core asp.net-identity

我有一个托管在localhost:9000运行的节点应用程序(与Express反应)。我正在为我的ASP.NET Core 2.0 Web项目制作一个axios REST POST,如下所示:

http://localhost:50494/rest/sessions

GET可以正常工作,但POST没有。在My Startup.cs文件中,我相信我已经设置了我的asp.net核心端点上允许的所有起源和方法,但我仍然得到了我认为的CORS未设置错误。

http://localhost:50494/rest/sessions/6184: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access.

这是我在asp.net端的设置:

Program.cs的

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}

Startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors();
        services.AddMvc();

    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {


        app.UseStaticFiles();

        app.UseCors(builder =>
                    builder.AllowAnyOrigin().AllowAnyMethod());

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

为什么我收到此错误?

0 个答案:

没有答案