ServiceStack CORS Feature

谁说胖子不能爱 提交于 2019-11-26 18:27:08

问题


Using the new Service implementation, do I have to provide an Options method for all of my services?

Using the old ServiceBase approach, which all my services currently use, OPTIONS returns OK without the Access-Control-Allow-Origin header.

Here's an example:

https://github.com/JonCanning/SSCors

HelloService uses Service

GoodbyeService uses ServiceBase


回答1:


Because ServiceStack's Old API enforced an interface-based API it only supported GET, POST, PUT, DELETE, PATCH requests. Handling OPTION requests we're essentially a stop-gap work-around that had a single implementation to just emit the configured headers and close the response.

With ServiceStack's New API there is no longer any limitation as you can now handle any HTTP Verb by just using its name on your IService. This now lets you handle all verbs to specific requests individually. But now it's no longer handled implicitly for you and need an implementation to handle it via a Service.

You can continue to handle all OPTIONS requests by using any of the pre-defined hooks to handle it generically before it reaches the Service.

E.g.

Plugins.Add(new CorsFeature()); //Registers global CORS Headers

this.RequestFilters.Add((httpReq, httpRes, requestDto) => {
   //Handles Request and closes Responses after emitting global HTTP Headers
    if (httpReq.HttpMethod == "OPTIONS") 
        httpRes.EndRequest();
});


来源:https://stackoverflow.com/questions/13741397/servicestack-cors-feature

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!