Redirecting to Metadata Page, mixing minimal APIs

What am I trying to achieve: when someone navigates to the root of the web host I want to redirect them to /{pathbase}/metadata. Pre ServiceStack 6 I was able to achieve this with the below code:

app.Run(context => 
{ 
   context.Response.Redirect("/{pathbase}/metadata"); 
   return Task.CompletedTask; 
 });

The above now silently fails so I’ve attempted to do the same using minimal apis:

var app = builder.Build();
app.MapGet("/", context =>
{
    context.Response.Redirect("/{pathbase}/metadata");
    return Task.CompletedTask;
});

When I comment out https://github.com/DeonHeyns/RedirectRepro/blob/main/RoutesNotCalled/Configure.AppHost.cs#L5

[assembly: HostingStartup(typeof(RoutesNotCalled.AppHost))]

Then the endpoint is hit.

Two questions:

  1. Is the above approach correct?
  2. If not is there a recommended approach?

Repro: https://github.com/DeonHeyns/RedirectRepro

Thanks,
Deon

When you want to combine different app handlers which ever gets defined first is what gets priority.

To specifically order the handlers, remove the .Configure(app => ...) from the AppHost and move them into Program.cs in the order you want them run, e.g:

var app = builder.Build();
app.MapGet("/", context =>
{
    context.Response.Redirect("/foo/metadata");
    return Task.CompletedTask;
});
app.UseServiceStack(new AppHost {
    PathBase = "/foo"
});

Awesome. I can confirm this works!

1 Like