MVC6 Cors-拦截飞行前
我正在将我的WebApi升级到MVC6 在WebApi中,我可以拦截每个HTTP请求,如果是预飞行,我可以用浏览器可以接受的标题进行响应 我试图弄清楚如何在MVC6WebAPI中做同样的事情 下面是WebApi代码MVC6 Cors-拦截飞行前,cors,asp.net-core-mvc,preflight,Cors,Asp.net Core Mvc,Preflight,我正在将我的WebApi升级到MVC6 在WebApi中,我可以拦截每个HTTP请求,如果是预飞行,我可以用浏览器可以接受的标题进行响应 我试图弄清楚如何在MVC6WebAPI中做同样的事情 下面是WebApi代码 protected void Application_BeginRequest(object sender, EventArgs e) { if (Context.Request.Path.Contains("api/") && Con
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (Context.Request.Path.Contains("api/") && Context.Request.HttpMethod == "OPTIONS")
{
Context.Response.AddHeader("Access-Control-Allow-Origin", Context.Request.Headers["Origin"]);
Context.Response.AddHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
Context.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
Context.Response.AddHeader("Access-Control-Allow-Credentials", "true");
Context.Response.End();
}
}
如何对MVC6执行相同的操作
谢谢,
鲍勃
下面是我基于反馈的下一次尝试。如果我理解中间件管道,我可能会自己解决这个问题。当然,我现在就要学
我尝试了这段代码,但并没有像我所希望的那样在http请求上成功
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// Configure the HTTP request pipeline.
app.UseStaticFiles();
// Add MVC to the request pipeline.
app.UseMvc();
// Add the following route for porting Web API 2 controllers.
// routes.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}");
// custom middleware to checked each call as it comes in.
app.Use(async (httpContext, next) =>
{
if (httpContext.Request.Path.Value.Contains("api/") && httpContext.Request.Method == "OPTIONS")
{
httpContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { httpContext.Request.Headers["Origin"] });
httpContext.Response.Headers.Add("Access-Control-Allow-Headers", new[] { "Origin, X-Requested-With, Content-Type, Accept" });
httpContext.Response.Headers.Add("Access-Control-Allow-Methods", new[] { "GET, POST, PUT, DELETE, OPTIONS" });
httpContext.Response.Headers.Add("Access-Control-Allow-Credentials", new[] { "true" });
return;
}
await next();
});
}
你可以加上你自己的。下面是一个内联添加的快速示例,但您也可以封装在类中:
app.Use(async (httpContext, next) =>
{
if (httpContext.Request.Path.Value.Contains("api/") && httpContext.Request.Method == "OPTIONS")
{
httpContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { httpContext.Request.Headers["Origin"] });
httpContext.Response.Headers.Add("Access-Control-Allow-Headers", new[] { "Origin, X-Requested-With, Content-Type, Accept" });
httpContext.Response.Headers.Add("Access-Control-Allow-Methods", new[] { "GET, POST, PUT, DELETE, OPTIONS" });
httpContext.Response.Headers.Add("Access-Control-Allow-Credentials", new[] { "true" });
return;
}
await next();
});
您可能还需要关注在ASP 5框架内支持cors的方法谢谢Daniel。我今天要试试这个。感谢您提供额外的“正在进行的工作”资源。我会注意的,这很有效。现在IIS正在劫持飞行前的选项。在WebAPI中,通过WebConfig更改很容易克服此问题。现在我们没有网络配置。。。还是我们?嗯,我会回来的。wwwroot文件夹中仍然有一个web.config,我希望该文件中的IIS设置能够继续工作(类似),但我自己只尝试过IIS express。4年零8个月后,我发现了这个响应,它帮了我大忙。在同一个包含MVC和Web Api控制器(后者通过令牌进行身份验证)的站点中结合OpenId和JWT令牌身份验证时,拦截预飞是我绕过浏览器CORS检查的唯一方法。非常感谢。复制