c# - Issue in Enabling CORS for Web API 1, .net 4.0 -
i need enable cors web api , can't upgrade framework 4.5. i've tried add following web.config see if worked, didn't:
<add name="access-control-allow-origin" value="*" /> <add name="access-control-allow-headers" value="accept,content-type,x-requested-with"/>
i accessing url http://localhost:8484/api/values/ ajax call
and getting bellow error
xmlhttprequest cannot load http://localhost:8484/api/values. response preflight request doesn't pass access control check: no 'access-control-allow-origin' header present on requested resource. origin 'null' therefore not allowed access. response had http status code 405.
i found simple solution charanjit singh. worked nicely if stuck older visual studio 2010 , on .net 4.0 , of course web api 1. add function global.asax.cs
protected void application_beginrequest(object sender, eventargs e) { httpcontext.current.response.addheader("access-control-allow-origin", "*"); if (httpcontext.current.request.httpmethod == "options") { httpcontext.current.response.addheader("access-control-allow-methods", "get, post, put, delete"); httpcontext.current.response.addheader("access-control-allow-headers", "content-type, accept"); httpcontext.current.response.end(); } }
ref. links: note must scroll bottom comments answer. http://www.codeguru.com/csharp/.net/net_asp/using-cross-origin-resource-sharing-cors-in-asp.net-web-api.html
Comments
Post a Comment