Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6851693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:16:45+00:00 2026-05-27T01:16:45+00:00

Does anyone know if the ServiceStack framework can be used to create CORS REST

  • 0

Does anyone know if the ServiceStack framework can be used to create CORS REST services?

I’ve been banging my head against the WCF REST stuff for days now – utterly useless.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:16:46+00:00Added an answer on May 27, 2026 at 1:16 am

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

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

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

    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 { ... }
    

    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.

    Setting Global HTTP Headers

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

    Returning Custom HTTP Headers in a service

    These headers will get sent on every request, alternatively you can also enable it for specific web services, i.e. take the Hello World Web Service for example:

    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" }, }
            };
        }
    }
    

    The above is all the C# code you need to develop a web service which is then automatically wired up for you on all HTTP Verbs (GET, POST, etc) and built-in endpoints, i.e. JSON, XML, JSV, HTML, CSV, SOAP 1.1/1.2 – for free, without any config or friction required. Checkout the live example of the above web service.

    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:

    http://www.servicestack.net/ServiceStack.Hello/servicestack/hello/world?callback=cb

    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.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know of any existing packages or libraries that can be used to
Does anyone know if I can create an HTML Form to search the DHL
Does anyone know where online copies of the old The Perl Journal articles can
Does anyone know how I can, in platform-independent C++ code prevent an object from
Does anyone know of any good tutorials on ADO.NET Entity Framework? There are a
Does anyone know how I can implement a single Touch Event. A simple, one
Does anyone know of a good library providing Ant tasks for performing operations against
Does anyone know if I can change a m3u8 file dynamically during runtime of
Does anyone know if I can find a list of where the line break
Does anyone know where I can find a tutorial on how to use GStreamer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.