Swashbuckle.webapi: Relative path for UI request URL-s

Created on 2 Jun 2014  路  4Comments  路  Source: domaindrivendev/Swashbuckle.WebApi

Hi,

Is it possible to turn on relative path resolver for Swagger UI request URL-s or can it be implemented ?

I have a server that uses many load balancers. And because of it I can get swagger UI index page work well, but when trying to call any method, request are made to wrong base path and port.

For example, using load balancer, index page can be found from:
http://localhost:8401/swagger/ui/index.html
But when making any query, following path is used:
http://localhost:8450/api/Status

Can you please support the chance to use relative paths that can be used instead of configured path (automatically or manually) on App_start register ?

Most helpful comment

Hello,

This is an old issue but I think this code will be more apprpriate:

c.RootUrl(req => req.RequestUri.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/").TrimEnd('/'));

Because currently, VirtualPathRoot don't work for me...
Swagger's Docs are generated correctly but SwaggerUI gets a wrong url (without the virtual path).

All 4 comments

Hi there.

Although it's not documented (my bad!), Swashbuckle provides a config option to override the way in which the basePath is resolved. I think you'll be able to use this to work around the issue:

SwaggerSpecConfig.Customize(c =>
    {
        c.ResolveBasePathUsing((req) => .... );
    }

The lambda takes the current HttpRequest (i.e. the request for a given Swagger ApiDeclaration) and should return a string to be used as the baseUrl for your Api. For load-balanced apps, this should return the load-balancer path.

The default implementation is as follows:

(req) => req.RequestUri.GetLeftPart(UriPartial.Authority) + req.GetConfiguration().VirtualPathRoot.TrimEnd('/');

Actually, until now I would have expected this to just work in a load balanced environment but your issue prompted me to investigate and lead me to the following blog http://www.rajeeshcv.com/post/details/40/stay-away-from-request-url. Now it's apparent that the default implementation should be modified to workaround this. I'll try get to it as soon as possible but in the meantime you should be able to implement a suitable override.

Re relative paths, the Swagger spec requires absolute paths because the URL at which the Swagger is being served need not be the URL of the actual API.

Let me know if this helps?

Thanks

Thank you for proper explanation how it is implemented.

But unfortunately I wasn't able to get it work. When trying to set 'ResolveBasePathUsing', HttpRequest is not available. So I cannot get load-balancer path. And for same reason, I cannot implement the solution shown on blog post. Maybe I have missed something and you have some good idea, how I can still get it work ?

Thanks

The lambda is passed a HttpRequestMessage instance ... you should be able to use this to get at the RequestUri etc. Another option, you could just place the host name in your web.config and have the lambda just read it from there.

Hello,

This is an old issue but I think this code will be more apprpriate:

c.RootUrl(req => req.RequestUri.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/").TrimEnd('/'));

Because currently, VirtualPathRoot don't work for me...
Swagger's Docs are generated correctly but SwaggerUI gets a wrong url (without the virtual path).

Was this page helpful?
0 / 5 - 0 ratings