C# 如何在ASP.NET MVC中启用跨源请求
我正在尝试创建一个在MVC5中处理跨源请求(COR)的web应用程序。我什么都试过了,没有结果 带有属性的C# 如何在ASP.NET MVC中启用跨源请求,c#,asp.net,asp.net-mvc,cors,C#,Asp.net,Asp.net Mvc,Cors,我正在尝试创建一个在MVC5中处理跨源请求(COR)的web应用程序。我什么都试过了,没有结果 带有属性的 public class AllowCrossSiteJsonAttribute: ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { filterContext.RequestContext.Ht
public class AllowCrossSiteJsonAttribute: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
base.OnActionExecuting(filterContext);
}
}
[EnableCors("*")]
带有EnableCors属性
public class AllowCrossSiteJsonAttribute: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
base.OnActionExecuting(filterContext);
}
}
[EnableCors("*")]
我开始认为在mvc 5(core)中启用CORS是不可能的,首先需要将Microsoft.AspNetCore.CORS包添加到项目中。然后像这样为所有网站配置startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
//Add Cors support to the service
services.AddCors();
var policy = new Microsoft.AspNet.Cors.Core.CorsPolicy();
policy.Headers.Add("*");
policy.Methods.Add("*");
policy.Origins.Add("*");
policy.SupportsCredentials = true;
services.ConfigureCors(x=>x.AddPolicy("AllPolicy", policy));
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// Configure the HTTP request pipeline.
app.UseStaticFiles();
//Use the new policy globally
app.UseCors("AllPolicy");
// Add MVC to the request pipeline.
app.UseMvc();
}
然后你也可以这样使用
[EnableCors("AllPolicy")]
详细信息在web.config文件中添加配置设置,以设置
customHeaders
中的Access Control Allow Origin
的值,如下所示-
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
更多细节和其他选项。我认为您必须将其添加到“OnAuthentication”步骤或将配置添加到web配置中。你可以试试我的代码:)它可以工作
要启用跨源请求,请将[EnableCors]
属性添加到Web API控制器或控制器方法:
[EnableCors(origins: "http://systematixindia.com", headers: "*", methods: "*")]
public class TestController : ApiController
{
// Controller method`enter code here`s not shown...
}
我认为最方便的方法是创建自己的类,如下所示:
其中包含以下代码:
using System;
using System.Web.Mvc;
public class AllowCrossSiteAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "http://localhost:4200");
filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Headers", "*");
filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Credentials", "true");
base.OnActionExecuting(filterContext);
}
}
在此之后,它允许您在方法上使用此装饰器或在整个控制器上使用此装饰器
完成此步骤后,您应该能够在响应标题中看到这一点
感谢这一点我成功地使用OWIN CORS实现(nuget Microsoft.OWIN.CORS)为MVC控制器和OWIN中间件以及APIController启用了CORS。Microsoft.AspNet.WebApi.Cors(使用config.EnableCors()
和[EnableCors]
属性)似乎只适用于APIController
有关示例代码,请参阅。您也可以使用下面的代码来允许跨原产地请求
public void ProcessRequest (HttpContext context)
{
context.Response.AddHeader("Access-Control-Allow-Origin" , "*");
}
你读过示例了吗:我在这篇文章中找到了解决方案:错误:EnableCors没有使用origins参数你正在使用哪个版本的asp.net mvc?哦,5件事是不同的,请查看我的updatecheck更新中的链接,我为mvc 5添加了此解决方案是为.net core mvc提供的,不适合mvc 5(.net framework)您的解决方案是针对Web Api的,请阅读问题的标题!它不适用于MVC。可以为全局配置公共静态类WebApiConfig{public static void Register(HttpConfiguration config){//To do:设置在全局位置ex web.config EnableCorsAttribute corsConfig=new EnableCorsAttribute(“,”,“GET,POST”);//Web API配置和服务配置.EnableCors(corsConfig);@OmarIsaid该类(WebAPIConfig.cs)在MVC项目中不存在您还应该包括filterContext.RequestContext.HttpContext.Response.AddHeader(“Vary”,“Origin”);
但我得到了错误,因为“EventSource的响应具有MIME类型(”不是“文本/事件流”的应用程序/json)。正在中止连接“它显示的结果如下:”来自源“”已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:它没有HTTP ok状态。“这段代码在哪里?如果不想使用通配符“*”,请在global.asax文件中找到最佳答案。