Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to enable CORS using NancyFX?

Tags:

cors

azure

nancy

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.

like image 929
Alberto Basalo Avatar asked Mar 27 '13 12:03

Alberto Basalo


People also ask

How do I enable CORS in REST API?

Enable CORS support on a REST API resourceSign in to the API Gateway console at https://console.aws.amazon.com/apigateway . Choose the API from the APIs list. Choose a resource under Resources. This will enable CORS for all the methods on the resource.

Is it a good practice to enable CORS?

CORS isn't bad practice. It is supported on all major browsers, and more and more APIs are supporting it. In fact, if you have a public resource that is not behind a firewall, it is safe to put the Access-Control-Allow-Origin: * header on the resource.

How do I enable CORS on my browser?

To enable cross-origin access go to Tools->Internet Options->Security tab, click on “Custom Level” button. Find the Miscellaneous -> Access data sources across domains setting and select “Enable” option.


1 Answers

Its possible to do this in the bootstraper of Nancy

protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)     {         //CORS Enable         pipelines.AfterRequest.AddItemToEndOfPipeline((ctx) =>         {             ctx.Response.WithHeader("Access-Control-Allow-Origin", "*")                             .WithHeader("Access-Control-Allow-Methods", "POST,GET")                             .WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type");          }); 
like image 64
oaamados Avatar answered Sep 30 '22 16:09

oaamados