2

What's the best way to determine the base URL path at which a ServiceStack instance is running? For example, if I configure ServiceStack to run at the "/api" base URL in web.config, how do I get the string "/api"? I would like to get this value in order to construct URLs of arbitrary request DTOs, as HATEOAS style documentation.

For example, if I have a request DTO SomeRequest with a [Route("/someRequest")] attribute, the full URL of this request would be "/api/someRequest". The ToUrl extension method returns just part of the path below the base URL:

new SomeRequest().ToUrl() // returns "/someRequest"

I haven't found any easy way to do this. It seems that there are no config properties exposed by ServiceStack that return the "/api" base path value. Inspecting web.config to get the ServiceStack handler path might be possible with ServiceStack 4 but I'm having trouble figuring out how to do it with our current SS 3 configuration.

The Request.GetAbsolutePath() method will produce the full path of the current request, e.g. "/api/someRequest", and then I could maybe do some string comparison between this and the ToUrl() extension method to determine the base path, but that also seems like a pretty fragile solution.

Is there any better way to get this base URL?

3
  • @Scott regarding the servicestack-bsd tag - we're going to use that instead of servicestack-v3? Commented Jan 15, 2014 at 17:37
  • I did try discussing this on meta. There was objection to a v3 specific tag in case stuff was relevant to versions less than 3 - though I don't think they really exist now. But BSD didn't seem offensive. I have been using it on new questions that explicitly apply to v3. Commented Jan 15, 2014 at 17:41
  • 1
    I think I like servicestack-bsd better anyway. Good to know. Commented Jan 15, 2014 at 17:44

1 Answer 1

5

I would combat this issue by checking for the prefix in the ServiceStack AppHost config. From the v3 documentation:

In order to avoid conflicts with your existing ASP.NET web framework it is recommended to host your ServiceStack web services at a custom path. ...

The location configuration (to your root Web.config file) below hosts your webservices at custom path: /api

...

You also need to configure the root path in your AppHost.

So because ServiceStack expects you to configure it with the matching path like this in the AppHost:

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

You can access the value directly from ServiceStack's config.

In v3 it can be accessed from the Service using:

GetAppHost().Config.ServiceStackHandlerFactoryPath

In v4 you can use:

AppHost.Instance.Config.WebHostUrl

Obviously it would be handy if the UrlExtensions ToUrl() method already performed this check and prefixed accordingly.


As @esker notes the path will not include the leading slash, so it must be prefixed with / to be a valid static path.

Sign up to request clarification or add additional context in comments.

2 Comments

Turns out I didn't have ServiceStackHandlerFactoryPath set. Setting this to "api" allowed me to construct the URLs as you have shown. Also this fixes the URLs reported on the metadata pages. One thing to note: if you try to set a ServiceStackHandlerFactoryPath value with a leading slash, like "/api", the slash will be stripped out. So when generating URLs using this value, it may be necessary to prepend with "/".
@esker That's a good point. I have added this note at the bottom.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.