2

I have a self-hosted application with many routes set up. Rather than going through each one and changing the route to be /api/<route> where <route> is the existing route, I was wondering if I can prefix each route with /api when I start the application? I know its possible in an IIS hosted enviroment by setting it in the web.config but I am not sure if its possible in a self-hosted environment?

CallumVass
  • 11,288
  • 26
  • 84
  • 154

2 Answers2

5

@EliGassert's answer is right for ServiceStack v4 self-hosted applications. This is the requirements to change the base path of all routes in a ServiceStack v3 self-hosted application.

ServiceStack v3:

In you AppHost Configure method, you need to set ServiceStackHanderFactoryPath to the desired prefix.

public override void Configure(Container container)
{
    SetConfig(new EndpointHostConfig {
        ServiceStackHandlerFactoryPath = "api"
    });
}

When you set your listener you must also append the prefix:

appHost.Start("http://*:9000/api/");

Hope that helps.

Community
  • 1
  • 1
Scott
  • 21,211
  • 8
  • 65
  • 72
  • Yeah I was talking with @Eli Gassert on Jabbr about this. I tried it and then my index.html page failed to load, returned 404 when I navigated to /index.html or /api/index.html hence why he made a note. – CallumVass May 14 '14 at 11:26
  • @BiffBaffBoff Ah ok, one sec, I reckon I can solve that too. I have an incline as to what that is. Let me just run a test. – Scott May 14 '14 at 11:27
  • Ideally I'd liek to serve the static HTML at root and then all my services on /api – CallumVass May 14 '14 at 11:31
  • @BiffBaffBoff I am not having an issue serving up the static html from the `/api` directory after adding the `ServiceStackHandlerFactoryPath = "api"`. So I see `/api/index.html`. But it won't be possible to have the index.html at the root i.e. `/index.html` and `/api` without manually prefixing your services, because of the way the AppHost listener works. But as I said `/api/index.html` should work given that `index.html` is in your executing directory. – Scott May 14 '14 at 11:44
  • @BiffBaffBoff You can configure the root directory programatically in the `EndPointHostConfig` using `WebHostPhysicalPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)` You could set it to any path the application can access. – Scott May 14 '14 at 11:45
  • I'm not sure, it wasn't/isn't working for me. I just did a find and replace to change all my `Routes` to `api/` is how I got around it.. – CallumVass May 14 '14 at 11:54
4

According To this ServiceStack article you just need to set it through the config, like this:

public override void Configure(Container container)
{
    SetConfig(new HostConfig { HandlerFactoryPath = "api" });
}

Combine that with this answer from Mythz and you got yourself a self-hosted app at /api/:

_apphost.Start("http://localhost:1337/api/");

Note: this seems to have worked for the self-hosted API, but then it fails to serve up its razor pages. So this isn't quite right. Still, leaving the answer for now until a full solution is found.

Community
  • 1
  • 1
Eli Gassert
  • 9,745
  • 3
  • 30
  • 39