【发布时间】: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