4

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

Sanket
  • 19,295
  • 10
  • 71
  • 82
Sigurd Garshol
  • 1,376
  • 3
  • 15
  • 36

1 Answers1

0

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");
markt
  • 5,126
  • 31
  • 25
  • That works, kind of. Removing the leading "/swagger" and "/TheGreatestAPI/swagger" did in fact result in a definition endpoint available at `https://localhost:5000/swagger/v1/swagger.json` and `https://test.contoso.com/TheGreatestWebAPI/swagger/v1/swagger.json`, respectively. However, the standard Swagger GUI page at `https://localhost:5000/swagger/index.html` tries to load from `https://localhost:5001/swagger/index.html/v1/swagger.json` - it essentially extends the GUI **file name** with the relative url rather than the **folder** – Sigurd Garshol Jul 20 '17 at 07:09
  • @SigurdGarshol I see.. I typically just go to `/swagger` and don't go directly to `/swagger/index.html` but if it doesn't work some of the time, it's definitely not ideal. You could alternatively just add a configuration value for the root of the site and pass that in when configuring swagger. – markt Jul 20 '17 at 13:57
  • Maybe it's just in the version of swagger we use right now, but /swagger redirects to /swagger/index.html automatically. So no workaround for us there. :( – Sigurd Garshol Jul 29 '17 at 08:23