Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.Net core 1.1 Angular2 PUT请求不抛出访问控制源_Angular_Asp.net Core - Fatal编程技术网

ASP.Net core 1.1 Angular2 PUT请求不抛出访问控制源

ASP.Net core 1.1 Angular2 PUT请求不抛出访问控制源,angular,asp.net-core,Angular,Asp.net Core,在我的ASP.NETCore1.1中。后端我已启用CORS,如下所示: public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddDbContext<WebAPIDataContext>(options => { options.UseMySql(Configuration.GetConnectio

在我的ASP.NETCore1.1中。后端我已启用CORS,如下所示:

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddDbContext<WebAPIDataContext>(options =>
    {
        options.UseMySql(Configuration.GetConnectionString("MysqlConnection"));
    });
    services.AddScoped<IProfileRepository, ProfileRepository>();
    services.AddScoped<IUser_TaskRepository, User_TaskRepository>();

    services.AddCors(options =>
    {
        options.AddPolicy("CorsPolicy",
            builder => builder.AllowAnyOrigin()
            .AllowAnyMethod()
            .AllowAnyHeader());
    });

    services.AddMvc();

    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
    });


}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    // global policy - assign here or on each controller
    app.UseCors("CorsPolicy");


    app.UseMvc();

    // Enable middleware to serve generated Swagger as a JSON endpoint.
    app.UseSwagger();

    // Enable middleware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint.
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
    });


}

我做错了什么?

第一步是检查调试控制台是否确实是PUT失败,或者是选项请求(浏览器预飞行)。如果选项是问题所在,您必须在后端做出反应。我不是asp.net core方面的专家,但在WebApi后端,您必须在global.asax.cs中执行此操作:

 protected void Application_BeginRequest()
    {
        if (Request.HttpMethod == "OPTIONS")
        {
            Response.StatusCode = (int)HttpStatusCode.OK;
            Response.AppendHeader("Access-Control-Allow-Origin", Request.Headers.GetValues("Origin")[0]);
            Response.AddHeader("Access-Control-Allow-Headers", "content-type, accept"); 
            Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
            Response.AppendHeader("Access-Control-Allow-Credentials", "true");
            Response.End();
        }
    }
如果选项不是您的问题,请仔细检查您在通话中输入的内容是否正确。如果您弄乱了调用所需的标题/参数,您经常会遇到此错误(这可能会非常混乱)

 protected void Application_BeginRequest()
    {
        if (Request.HttpMethod == "OPTIONS")
        {
            Response.StatusCode = (int)HttpStatusCode.OK;
            Response.AppendHeader("Access-Control-Allow-Origin", Request.Headers.GetValues("Origin")[0]);
            Response.AddHeader("Access-Control-Allow-Headers", "content-type, accept"); 
            Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
            Response.AppendHeader("Access-Control-Allow-Credentials", "true");
            Response.End();
        }
    }