I have an API service made with NancyFX, and a couple of front-end developers creating an SPA JS client against this API.
We would like to test the client side code against the published server without having to publish the client code with too much frequency.
But, the client runs at localhost, and the server is at Windows Azure.
Is it possible and easy to enable CORS on the NancyFX server? How can I do that?
Thanks.
If you're using IIS to host Nancy, in this case on Windows Azure then you can just update the web.config to add the header to every request.
This can be done by adding the following:
Alternatively you can do what Sunny suggested, and if you don't like writing that every time you can add your own extension method:
Then you can just call
this.EnableCors()
in your route.Its possible to do this in the bootstraper of Nancy
If your HTTP request is simple then Phill's answer will suffice, but if the request is not so simple, the browser will send a preflight check. The preflight check is an OPTIONS HTTP request and this has to be handled too.
Here is an extension method to configure CORS:
To enable CORS call this extension method in the bootstrapper:
Please note it is not extending NancyModule because OPTIONS is handled outside of module (also here).