servicestack REST API和CORS [英] servicestack REST API and CORS

查看:218
本文介绍了servicestack REST API和CORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道,如果servicestack框架可以用来创建CORS REST服务?

Anyone know if the servicestack framework can be used to create CORS REST services?

我一直在敲打我现在haed对WCF REST工具和天 - 完全无用

I've been banging my haed against the WCF REST stuff for days now - utterly useless.

感谢

推荐答案

我们现在有一个 CorsFeature 它包装CORS头到插件来使其更容易CORS支持添加到您的ServiceStack服务。

Using the CorsFeature plugin

Enabling Global CORS support

We now have a CorsFeature which wraps CORS headers into the Plugin below to make it much easier to add CORS support to your ServiceStack services.

通常这是现在所有的需要:

Commonly this is now all that's needed:

Plugins.Add(new CorsFeature());

这将使用默认值:

Which uses the default values:

CorsFeature(allowedOrigins:"*", 
    allowedMethods:"GET, POST, PUT, DELETE, OPTIONS", 
    allowedHeaders:"Content-Type", 
    allowCredentials:false);

您可以将任何匹配的默认值的。例如。如果你只是想限制所允许的方式,只是GET和POST请求,你可以做:

You can leave out any of the values matching the default. E.g. if you just wanted to restrict the allowed methods to just GET and POST requests, you can just do:

Plugins.Add(CorsFeature(allowedMethods:"GET, POST"));

全局使CORS所有选项请求

一旦CorsFeature(或手动全局头部)注册,您也可以选择启用CORS所有选项加入了preRequest过滤器发出的所有注册的全局头部请求(即在CorsFeature头)和短路所有的选项请求有:

Globally enable CORS for all OPTION requests

Once the CorsFeature (or manual Global Headers) is registered, you can optionally choose to enable CORS for all OPTION requests by adding a PreRequest filter to emit all registered Global Headers (i.e. the Headers in CorsFeature) and short-circuit all OPTIONS requests with:

this.PreRequestFilters.Add((httpReq, httpRes) => {
    //Handles Request and closes Responses after emitting global HTTP Headers
    if (httpReq.Method == "OPTIONS") 
        httpRes.EndRequest(); //add a 'using ServiceStack;'
});

启用CORS每个服务支持

除了使用上面的插件,也ServiceStack允许使用的 [EnableCors] 响应滤波器属性具有与上述相同的默认值。例如。你可以只启用GET,POST如上:

Enabling CORS per-service support

Instead of using the plugin above, ServiceStack also allows you to enable CORS on a per-service basis by using [EnableCors] Response Filter attribute which has the same defaults as above. E.g. You can enable just GET, POST as above with:

[EnableCors(allowedMethods:"GET,POST")]
public class MyService : Service { ... }

手动启用CORS

ServiceStack 的美妙之处在于它是建立在一个高度灵活且简单的核心。我们不试图通过一切打造强类型的API,因为这是不可能predict什么新的HTTP标头/状态codeS将在未来存在。因此,虽然我们提供方便的行为来完成共同的任务,我们也提供了灵活的API,可以让你配置所需的任何HTTP输出。

Manually enabling CORS

The beauty of ServiceStack is that it's built on a highly flexible and simple core. We don't try to build strong-typed APIs over everything, as it's impossible to predict what new HTTP Headers / StatusCodes will exist in the future. So whilst we provide convenient behavior to accomplish common tasks, we also provide a flexible API that lets you configure any desired HTTP Output.

这是如何在全球范围内,你APPHOST配置启用跨地共享:

This is how to globally enable Cross Origin Sharing in you AppHost config:

public override void Configure(Container container)
{
    //Permit modern browsers (e.g. Firefox) to allow sending of any REST HTTP Method
    base.SetConfig(new EndpointHostConfig
    {
        GlobalResponseHeaders = {
            { "Access-Control-Allow-Origin", "*" },
            { "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS" },
            { "Access-Control-Allow-Headers", "Content-Type" },
        },
    });
}

返回自定义HTTP头在服务

这些头将得到在每次请求发送,或者你也可以启用它为特定的Web服务,即采取的Hello World网站例如服务

public class Hello {
    public string Name { get; set; }
}

public class HelloResponse {
    public string Result { get; set; }
}

public class HelloService : IService 
{
    public object Any(Hello request)
    {
        var dto = new HelloResponse { Result = "Hello, " + request.Name };
        return new HttpResult(dto) {
            Headers = {
              { "Access-Control-Allow-Origin", "*" },
              { "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS" } 
              { "Access-Control-Allow-Headers", "Content-Type" }, }
        };
    }
}

以上是所有的C#code你需要开发,然后自动在所有HTTP动词有线为您的Web服务(GET,POST等),内置的端点,即JSON,XML, JSV,HTML,CSV,SOAP 1.1 / 1.2 - 免费,无需任何配置或摩擦。结帐上述Web服务的活生生的例子。

除了以上端点的每个服务是提供给被JSONP(另一种流行的方式,使在Ajax应用程序跨域服务电话),其中每个服务可以通过JSONP调用,只需添加?回调称为= CB 参数查询字符串,例如:

In addition to the above endpoints each service is available to be called by JSONP (another popular way to enable cross-domain service calls in Ajax apps) where each service can be called via JSONP by simply adding the ?callback=cb parameter to the querystring, e.g:

<一个href=\"http://www.servicestack.net/ServiceStack.Hello/servicestack/hello/world?callback=cb\">http://www.servicestack.net/ServiceStack.Hello/servicestack/hello/world?callback=cb

这是使用ServiceStack的灵活性和生产率的胜利,你从字面上给无摩擦的灵活性和前pressive自由在你的web服务的另一个例子<一个href=\"http://stackoverflow.com/questions/6245616/does-servicestack-support-binary-responses\">literally返回几乎任何预期,它被序列化。

This is another example of the flexibility and productivity wins of using ServiceStack where you're literally given friction-free flexibility and expressive freedom in your web service to literally return just about anything and it gets serialized as expected.

这不仅更容易比WCF使用(有更多的功能外的开箱),但它也快很多,其中其组件是高度为最高性能优化。

It's not only easier to use than WCF (with more features out-of-the-box) but it's also much faster where all its components are highly optimized for maximum performance.

这篇关于servicestack REST API和CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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