【发布时间】:2015-12-17 14:33:14
【问题描述】:
我在 Azure 中托管了 ASP.NET 5 网站 (dnx451)。 部署后一个小时左右可以正常工作,然后 CORS 功能停止工作。 DI、授权和 MVC 等其他服务仍在运行。
这里是 Startup.cs 代码:
public void ConfigureServices(IServiceCollection services)
{
configureCors(services);
services.AddMvc(options =>
{
options.Filters.Add(new ErrorFilter());
});
}
private static void configureCors(IServiceCollection services)
{
services.AddCors();
services.ConfigureCors(x => x.AddPolicy("allowAll", p=>p.AllowAnyOrigin().
AllowAnyHeader().AllowAnyMethod()));
}
应用配置:
public void Configure(IApplicationBuilder app, IApplicationEnvironment env, ILoggerFactory loggerFactory)
{
app.UseMiddleware<StaticFileMiddleware>(new StaticFileOptions());
app.UseErrorPage();
// Add MVC to the request pipeline.
app.UseMvc();
app.UseCors("allowAll");
}
本地版本一直都有 CORS,但 Azure Web 仅在前 1-2 小时内有(取决于使用情况)。我猜当 Azure 重新启动应用程序时,CORS 会消失。
这很奇怪。有没有人有同样的问题?
【问题讨论】:
-
在
UseMvc()上方移动app.UseCors("allowAll");有帮助吗? -
在这种情况下,我在 Microsoft.AspNet.Cors.Core.CorsService.ApplyResult -> Microsoft.AspNet.Loader.IIS.HeaderCollection.Add(String key, String[ ] value) 移到上面:` app.UseCors("allowAll"); app.UseMvc(); `
-
我的意思是 MVC 中间件 (
UseMvc()) 通常会结束请求(例如不调用_next())所以添加 CORS after MVC 可能会导致不应用CORS 中间件。 -
你应该使用其中之一。因此,
[EnableCors]属性 或UseCors()将给定键添加到内部字典中。这可能解释了重复键异常。 -
仍然不确定奇怪的行为。但是以正确的顺序使用中间件并且不启用 CORS 两次可能会对您有所帮助。
标签: asp.net cors azure-web-app-service asp.net-core