Home > Software design >  Servicestack redirect to metadata
Servicestack redirect to metadata

Time:02-08

i've the following problem: i've a web service application that uses ServiceStack. I'd like to register as base path "/api", but even if I set DefaultRedirectPath to "/api/metadata", when i start the app it won't redirect automatically (if i type "/api/metadata" all works) Can anyone help me?
Here's my code inside AppHost

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



public class Startup
{
    public IConfiguration Configuration { get; }

    public Startup()
    {
        Configuration = new ConfigurationBuilder()
          .AddJsonFile("appsettings.json")
          .Build();
    }

    public void ConfigureServices(IServiceCollection services)
    {

    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseServiceStack(new AppHost
        {
            PathBase = "/api",
            AppSettings = new NetCoreAppSettings(Configuration)
        });
    }
}

Thanks in advance and sorry for my english

CodePudding user response:

Firstly I'd consider not using an /api PathBase which would disable the new /api route. E.g. if you didn't have a /api PathBase you would automatically be able to call a Hello API from /api/Hello.

But if you you still want to host ServiceStack at a custom /api path know that this is the path that ServiceStack will be mounted at, i.e. from where ServiceStack will be able to receive any requests.

Which also means you should just use /metadata which will redirect from where ServiceStack is mounted at, so if you had:

public class AppHost : AppHostBase
{
    public AppHost() : base("MyApp", typeof(MyServices).Assembly) {}

    public override void Configure(Container container)
    {
        SetConfig(new HostConfig {
            DefaultRedirectPath = "/metadata"
        });
    }
}

Then calling https://localhost:5001/api will redirect to https://localhost:5001/api/metadata.

ServiceStack can only see requests from /api where it's mounted at, so if you wanted to redirect the / route to the metadata page you would need to register a custom ASP.NET Core handler to do it, e.g:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseServiceStack(new AppHost {
        PathBase = "/api",
    });

    app.UseRouting();

    app.UseEndpoints(endpoints => {
        endpoints.MapGet("/", async context => 
            context.Response.Redirect("/api/metadata"));
    });
}

Note: you no longer need to set NetCoreAppSettings() which is populated by default

  •  Tags:  
  • Related