.NET Core 3.1 custom api path metadata

Hello mythz!

I try to host a .NET Core 3.1 service with Kestrel. I would like to set /api custom path for the service.
This is the setup of my HostConfig:

WebHostUrl = "http://localhost:8088/api",
HandlerFactoryPath = "api",
ApiVersion = "1",
DefaultRedirectPath = "~/metadata"

The service works, but after start the service the browser opens the http://localhost:8088/ url instead of http://localhost:8088/api/metadata url. How can I setup the host for the correct metadata page?

Thank You!

Typically you’d use a reverse proxy like nginx to map your preferred URL and path to your internal .NET Core process.

Otherwise path mapping in .NET Core has been changed to require specifying the path with UsePathBase(), e.g:

public void Configure(IApplicationBuilder app)
{
    app.UsePathBase("/api");
}

I tried to set UsePathBase but it didn’t work for me.

I think I got it.

public void Configure(IApplicationBuilder app)
{
  app.Use((context, next) =>
  {
      context.Request.PathBase = "/api";
      return next();
  });
}

That’s odd as app.UsePathBase("/api") should be registering middleware that does the same thing.

Anyway I’ve revisited this and looking to add an “official API” to make it easy to register the PathBase in ASP.NET Core Apps where the only thing you should need to do is specifying the PathBase when registering your AppHost, e.g:

app.UseServiceStack(new AppHost
{
    PathBase = "/api",
    AppSettings = new NetCoreAppSettings(Configuration)
});

I’ve also added deep support into all the UI server controls in Unified Navigation to auto include the PathBase if specified. Both Razor and #Script Pages now include a PathBase variable you can use to be able to generate links that can be used whether or not there’s a PathBase configured:

<a href="{{PathBase}}/metadata/nav">/metadata/nav</a>

This change is available in the latest v5.8.1 that’s now available on MyGet.

I’d appreciate if you could look at using the new official API and let me know anything that still doesn’t work right.

The API works, but the metadata page remains http://localhost:8088 at startup. If I type the full url http://localhost:8088/api the metadat page load correctly.

This is my DefaultRedirectPath setting: DefaultRedirectPath = "~/metadata"

Not exactly sure what you mean, if you host ServiceStack on a path it wont be handling requests to the base URL anymore.

If you want to you can add a catchall middleware after registering ServiceStack AppHost if you want to redirect any requests not handled by ServiceStack, e.g:

app.UseServiceStack(new AppHost {
    PathBase = "/api",
    AppSettings = new NetCoreAppSettings(Configuration)
});

app.Run(async context => {
   context.Response.Redirect("/api/metadata"); 
});