是否可以使用NancyFX启用CORS? [英] Is it possible to enable CORS using NancyFX?

查看:631
本文介绍了是否可以使用NancyFX启用CORS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用NancyFX的API服务,以及几个前端开发人员针对此API创建SPA JS客户端。

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.

但是,客户端在localhost运行,并且服务器在Windows Azure。

But, the client runs at localhost, and the server is at Windows Azure.

是否可以并且容易在NancyFX服务器上启用CORS?

Is it possible and easy to enable CORS on the NancyFX server? How can I do that?

感谢。

推荐答案

在Nancy的引导程序中这样做

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");

        });

这篇关于是否可以使用NancyFX启用CORS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆