3
votes

I would like to be able to register certain url's that take into account actual url of the resources. Specifically: the Swagger end point for my Web API documentation.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseMvc();

     app.UseSwagger();
     app.UseSwaggerUi(c =>
     {
         c.SwaggerEndpoint($"/swagger/v1/swagger.json", "API documentation");
         //c.SwaggerEndpoint($"/TheGreatestWebAPI/swagger/v1/swagger.json", "API documentation");
     });
}

In my development environment I will be hosting the Web PI on http://localhost:5000, in which case Swagger wants to talk to its endpoint here:

http://localhost:5000/swagger/v1/swagger.json

In a production or staging environment, the Web API will be hosted as a web application in IIS at something like https://test.contoso.com/TheGreatestWebAPI and swagger will want to talk to this:

https://test.contoso.com/TheGreatestWebAPI/swagger/v1/swagger.json

...hence the latter commented-out end point registration.

What I would like to do is to use something like this:

c.SwaggerEndpoint($"~/swagger/v1/swagger.json", "API documentation");

This should be familiar to anyone who's used ASP.NET web forms' Page.ResolveUrl(), and from what I can see the same functionality should be available through UrlHelper.Content(). However, the function requires an instance of the UrlHelper class, and the relevant constructor requires ActionContext instance.

I am at a loss as to how properly instantiate UrlHelper in this context (the UseSwaggerUI() in Startup.Configure()).

-S

1

1 Answers

0
votes

Documentation for the method says that the path can be fully-qualified or relative to the page.

In my case, it was enough to just remove the first part of the path:

c.SwaggerEndpoint("v1/swagger.json", "API documentation");