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.
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("*")]
Nothing works I'm starting to think that it is impossible
You can enable CORS per action, per controller, or globally for all Web API controllers in your application. To enable CORS for a single action, set the [EnableCors] attribute on the action method.
CORS Stands for Cross-Origin Resource Sharing. It is a W3C Standard that allows a server to relax the same-policy. Using CORS, a server can explicitly allow some cross-origin requests while rejecting others.
Add the configuration setting in your web.config file to set the value for Access-Control-Allow-Origin
in customHeaders
like this -
<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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With