ServiceStack CORS Feature

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();
});

Leave a Comment