Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ServiceStack CORS Feature

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

like image 777
Jon Canning Avatar asked Feb 19 '23 06:02

Jon Canning


1 Answers

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();
});
like image 148
mythz Avatar answered Mar 16 '23 18:03

mythz