This question already has an answer here:
这个问题已经有了答案:
- Setting Access-Control-Allow-Origin in ASP.Net MVC - simplest possible method 10 answers
- 设置Access-Control-Allow-Origin ASP。最简单的方法10答案
I am trying to create a web application which works with cross-origin requests (CORS) in MVC 5. I have tried everything without any result.
我正在尝试创建一个web应用程序,它可以在MVC 5中使用跨源请求(CORS)。我已经尽力了,但毫无结果。
With an attribute
与一个属性
public class AllowCrossSiteJsonAttribute: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
base.OnActionExecuting(filterContext);
}
}
With EnableCors attribute
与EnableCors属性
[EnableCors("*")]
Nothing works I'm starting to think that it is impossible
我开始认为这是不可能的
7 个解决方案
#1
2
To enable cross-origin requests, add the [EnableCors]
attribute to your Web API controller or controller method:
要启用跨源请求,请将[EnableCors]属性添加到您的Web API控制器或控制器方法:
[EnableCors(origins: "http://systematixindia.com", headers: "*", methods: "*")]
public class TestController : ApiController
{
// Controller method`enter code here`s not shown...
}
阅读更多
#2
18
What I think is the most convenient is to create your own class like this :
我认为最方便的是创建这样的类:
with the following code in it :
包含以下代码:
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);
}
}
After this it's let you use this decorator on a method or on the whole controller
在此之后,您可以在方法或整个控制器上使用这个decorator
You should be able to see that in your response header after this procedure
您应该能够在这个过程之后的响应头中看到这一点
Thank's to this response
谢谢这个反应
#3
11
Add the configuration setting in your web.config file to set the value for Access-Control-Allow-Origin
in customHeaders
like this -
在web中添加配置设置。配置文件来设置像这样的自定义头中的访问控制允许值
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
You would like to visit this and this for more details and some other options.
你想访问这个和这个更多的细节和一些其他的选择。
#4
0
I think you have to add it into "OnAuthentication" step or add config into your web config. You can try my code :) it works
我认为您必须将它添加到“OnAuthentication”步骤中,或者将配置添加到web配置中。你可以试试我的代码:)它工作
public class AllowCrossSiteJsonAttribute : ActionFilterAttribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
{
filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
}
}
#5
0
I've had success using the OWIN CORS implementation (nuget Microsoft.Owin.Cors) to enable Cors for MVC Controllers and Owin middleware, in addition to ApiControllers. Microsoft.AspNet.WebApi.Cors (using config.EnableCors()
and the [EnableCors]
attribute) only seems to work with ApiControllers.
除了ApiControllers之外,我还成功地使用了OWIN CORS实现(nuget Microsoft.Owin.Cors)来为MVC控制器和OWIN中间件启用CORS。Microsoft.AspNet.WebApi。Cors(使用config.EnableCors()和[EnableCors]属性)似乎只对ApiControllers起作用。
See http://benfoster.io/blog/aspnet-webapi-cors for sample code.
见http://benfoster。io /博客/ aspnet-webapi-cors示例代码。
#6
0
You can also use below code to allow cross-origin request
您还可以使用下面的代码来允许跨源请求
public void ProcessRequest (HttpContext context)
{
context.Response.AddHeader("Access-Control-Allow-Origin" , "*");
}
#7
-1
Enabling CORS in mvc 5(core) first need to add Microsoft.AspNetCore.Cors
package to your project. Then configure startup.cs
like this for all website
在mvc 5(核心)中启用CORS首先需要添加Microsoft.AspNetCore。Cors包到你的项目。然后配置启动。所有网站都是这样的
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();
}
and then also you can use like this
然后你也可以像这样使用
[EnableCors("AllPolicy")]
Details here
细节
#1
2
To enable cross-origin requests, add the [EnableCors]
attribute to your Web API controller or controller method:
要启用跨源请求,请将[EnableCors]属性添加到您的Web API控制器或控制器方法:
[EnableCors(origins: "http://systematixindia.com", headers: "*", methods: "*")]
public class TestController : ApiController
{
// Controller method`enter code here`s not shown...
}
阅读更多
#2
18
What I think is the most convenient is to create your own class like this :
我认为最方便的是创建这样的类:
with the following code in it :
包含以下代码:
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);
}
}
After this it's let you use this decorator on a method or on the whole controller
在此之后,您可以在方法或整个控制器上使用这个decorator
You should be able to see that in your response header after this procedure
您应该能够在这个过程之后的响应头中看到这一点
Thank's to this response
谢谢这个反应
#3
11
Add the configuration setting in your web.config file to set the value for Access-Control-Allow-Origin
in customHeaders
like this -
在web中添加配置设置。配置文件来设置像这样的自定义头中的访问控制允许值
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
You would like to visit this and this for more details and some other options.
你想访问这个和这个更多的细节和一些其他的选择。
#4
0
I think you have to add it into "OnAuthentication" step or add config into your web config. You can try my code :) it works
我认为您必须将它添加到“OnAuthentication”步骤中,或者将配置添加到web配置中。你可以试试我的代码:)它工作
public class AllowCrossSiteJsonAttribute : ActionFilterAttribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
{
filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
}
}
#5
0
I've had success using the OWIN CORS implementation (nuget Microsoft.Owin.Cors) to enable Cors for MVC Controllers and Owin middleware, in addition to ApiControllers. Microsoft.AspNet.WebApi.Cors (using config.EnableCors()
and the [EnableCors]
attribute) only seems to work with ApiControllers.
除了ApiControllers之外,我还成功地使用了OWIN CORS实现(nuget Microsoft.Owin.Cors)来为MVC控制器和OWIN中间件启用CORS。Microsoft.AspNet.WebApi。Cors(使用config.EnableCors()和[EnableCors]属性)似乎只对ApiControllers起作用。
See http://benfoster.io/blog/aspnet-webapi-cors for sample code.
见http://benfoster。io /博客/ aspnet-webapi-cors示例代码。
#6
0
You can also use below code to allow cross-origin request
您还可以使用下面的代码来允许跨源请求
public void ProcessRequest (HttpContext context)
{
context.Response.AddHeader("Access-Control-Allow-Origin" , "*");
}
#7
-1
Enabling CORS in mvc 5(core) first need to add Microsoft.AspNetCore.Cors
package to your project. Then configure startup.cs
like this for all website
在mvc 5(核心)中启用CORS首先需要添加Microsoft.AspNetCore。Cors包到你的项目。然后配置启动。所有网站都是这样的
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();
}
and then also you can use like this
然后你也可以像这样使用
[EnableCors("AllPolicy")]
Details here
细节