Best way to have all everything at /api except content pages

I want all service calls to go to /api but the content pages should work at the root. So “http://www.example.com” would render the content pages HTML but everything else in SS should be at “http://www.example.com/api”.

I have been using GetRoutesAttributes to add the /api prefix. This works for the custom routes but has some problems.

Is it possible to host two ServiceStack instances together (like can be done with MVC and SS)? I’m thinking one could handle the content pages and the other could be hosted at /api (again like you would if it as MVC and SS).

There can only be 1 ServiceStack Instance running in the same AppDomain, if you want 2 ServiceStack instances running at different paths from the same domain you’ll need to use a Reverse Proxy to proxy to 2 different ServiceStack hosts.

Sounds like the issues you’re running into is wanting to host ServiceStack at /api but you’re hosting it at / and trying to fake the /api with a route prefix which you want to behave like a BaseUrl instead of a route prefix - that’s not going to work.

I’d configure ServiceStack to be mounted at /api path and just use ASP.NET or MVC to serve content from /, which means you wont have access to ServiceStack.Razor but is still a good option for SPA’s with static .html or MVC dynamic pages.

Otherwise the other option is to split ServiceStack in 2 hosts with the Web Services served from api.example.org and the content served from www.example.org, in which case you’ll need to enable CORS if you want to call it from JavaScript/TypeScript.

Otherwise my preferred option is to just have a single ServiceStack instance without the /api prefix.

1 Like

Thanks, Demis. Using ASP.NET to host my SPA will work for now.

For other readers:

I’ve also had to set the WebHostPhysicalPath to "~".MapServerPath() + "\\api". This prevents /api serving the static content that I want ASP.NET to handle.

If you’re using Compiled Razor Views any cshtml files that shoudn’t be served by SS need the build action changing to None.

2 Likes