Pet*_*hev 25 c# asp.net asp.net-mvc cors
我正在尝试创建一个在MVC 5中使用跨源请求(CORS)的Web应用程序.我已经尝试了所有内容而没有任何结果.
带有属性
public class AllowCrossSiteJsonAttribute: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
base.OnActionExecuting(filterContext);
}
}
Run Code Online (Sandbox Code Playgroud)
使用EnableCors属性
[EnableCors("*")]
Run Code Online (Sandbox Code Playgroud)
什么都行不通我开始认为这是不可能的
Fit*_*tch 35
我认为最方便的是创建自己的类,如下所示:
使用以下代码:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
在此之后,它允许您在方法或整个控制器上使用此装饰器
在此过程之后,您应该能够在响应标头中看到它
谢谢你的回复
Yog*_*ogi 31
在web.config文件中添加配置设置以设置in的值Access-Control-Allow-Origin,customHeaders如下所示 -
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)
小智 -6
要启用跨源请求,请将该[EnableCors]属性添加到您的 Web API 控制器或控制器方法:
[EnableCors(origins: "http://systematixindia.com", headers: "*", methods: "*")]
public class TestController : ApiController
{
// Controller method`enter code here`s not shown...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
65531 次 |
| 最近记录: |