c# - Enable CORS in Web API 2 -
i have client , server running on different ports. server running web api 2 (v5.0.0-rc1).
i tried installing microsoft asp.net web api cross-origin support package , enabled in webapiconfig.cs. gives me enablecors() function, package installed correctly.
here can see register() function in webapiconfig.cs:
public static void register(httpconfiguration config) { config.maphttpattributeroutes(); var cors = new enablecorsattribute("*", "*", "*"); config.enablecors(cors); } get requests work fine. when sending post, following:
options http://localhost:19357/api/v1/rooms? 404 (not found) angular.js:10159 options http://localhost:19357/api/v1/rooms? origin http://localhost:8000 not allowed access-control-allow-origin. angular.js:10159 xmlhttprequest cannot load http://localhost:19357/api/v1/rooms. origin http://localhost:8000 not allowed access-control-allow-origin. according fiddler sends options request. doesn't issue post afterwards.
so i'm guessing config.enablecors(cors); in webapiconfig.cs isn't doing anything, leads server denying client/browser send post request.
do have idea how solve problem?
edit 05.09.13 has been fixed in 5.0.0-rtm-130905
cors works absolutely fine in microsoft.aspnet.webapi.cors version 5.2.2. following steps configured cors charm me:
install-package microsoft.aspnet.webapi.cors -version "5.2.2"// run package manager consolein global.asax, add following line: before mvc route registrations
globalconfiguration.configure(webapiconfig.register);in
webapiconfigregister method, have following code:public static void register(httpconfiguration config) { config.enablecors(); config.maphttpattributeroutes(); }
in web.config, following handler must first 1 in pipeline:
<add name="extensionlessurlhandler-integrated-4.0" path="*." verb="*" type="system.web.handlers.transferrequesthandler" precondition="integratedmode,runtimeversionv4.0" /> in controller derived apicontroller, add enablecorsattribute:
[enablecors(origins: "*", headers: "*", methods: "*")] // tune needs [routeprefix("")] public class mycontroller : apicontroller that should set nicely!
Comments
Post a Comment