【问题标题】:Why does ASP.NET Core 5 Web API prevent HttpPost request access?为什么 ASP.NET Core 5 Web API 会阻止 HttpPost 请求访问?
【发布时间】:2022-02-02 22:01:56
【问题描述】:

我在我的Startup.cs 文件中配置了一个Configure 方法,如下所示:

app.UseCors(option => option
                         .AllowAnyOrigin()
                         .AllowAnyHeader()
                         .AllowAnyMethod() );

但是在发布项目并将文件上传到 Plesk 时,HttpPost 方法根本不起作用,我收到此错误:

跨域请求被阻止:同源策略不允许读取远程资源(原因:CORS 标头“Access-Control-Allow-Origin”缺失)。状态码:500。

我之前设置过AllowAnyHeader(),但它的命令似乎不起作用。为什么 - 我该如何解决这个问题?

编辑:

我在 Web.config 中截断了这些:

  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
      </customHeaders>
    </httpProtocol>
    <security>
      <requestFiltering>
        <verbs>
          <remove verb="OPTIONS" />
        </verbs>
      </requestFiltering>
    </security>
  </system.webServer>

但是整个site 不会运行。

编辑:

我在 Startup.cs 中使用了这些片段:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers(); 
            services.AddControllers()
             .AddJsonOptions(o => o.JsonSerializerOptions
                 .ReferenceHandler = ReferenceHandler.Preserve);

            services.AddMemoryCache();

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

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            
            app.UseRouting();
            app.UseCors("CorsPolicy");
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }

【问题讨论】:

    标签: asp.net-core-mvc asp.net-core-webapi asp.net-core-5.0


    【解决方案1】:

    你使用了错误的语法,试试这个

    services.AddCors(opt =>  opt.AddPolicy("CorsPolicy", c =>
                { 
                    c.AllowAnyOrigin()
                       .AllowAnyHeader()
                        .AllowCredentials()
                        .AllowAnyMethod();
                }));
     ....
    
     app.UseCors("CorsPolicy") 
    

    【讨论】:

    • 感谢您的回复,但它不会工作
    • @albert 你必须发布你的启动,因为这段代码应该在特殊的地方。我想看看你把它放在哪里。
    • 请查看最后的编辑。
    • @albert 我看到 AddControllers 翻了一番。恕我直言,您必须删除第一个。
    • 哦,是的,非常同意你的看法,非常感谢
    【解决方案2】:

    发布项目和上传文件到 Plesk 时,HttpPost 方法根本不起作用

    如果请求超过maxAllowedContentLength 限制,即使我们启用了 CORS,也会导致问题。

    请检查浏览器客户端上传大文件时是否出现此问题。并且您可以尝试根据您的实际情况在您的网络服务器上增加maxAllowedContentLength 的值,然后检查它是否有助于解决问题。

    <requestFiltering>
        <requestLimits maxAllowedContentLength="90000000" />
    </requestFiltering>
    

    【讨论】:

    • 我已经编辑了上面的帖子,请检查它。 tnx
    【解决方案3】:

    感谢Serge

    根据 Serge 的帮助,services.AddControllers() 是重复的。 我删除了其中一个,项目正常运行。

    【讨论】:

      猜你喜欢
      • 2019-04-09
      • 2020-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-28
      • 2018-04-19
      相关资源
      最近更新 更多