Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swagger navigates to wrong UI URL when using Virtual directory in Owin enabled asp.net #1292

Open
pjc2007 opened this issue Jan 21, 2019 · 10 comments

Comments

@pjc2007
Copy link

pjc2007 commented Jan 21, 2019

VERSION:

Provide the version of Swashbuckle you're using

<package id="Swashbuckle" version="5.6.0" targetFramework="net47" />
<package id="Swashbuckle.Core" version="5.6.0" targetFramework="net47" /

STEPS TO REPRODUCE:

I have an asp.net webApi application, where I have just installed Swashbuckle. Very similar to this issue when I navigate to http://localhost/myapp/swagger is redirects to http://localhost/swagger/ui/index. If I manually then insert the virtual directory, ie myapp, it works, ie if I manually enter http://localhost/myapp/swagger/ui/index I get the UI that displays my routes.

I have the following setup code

//Startup.cs..

//GlobalConfiguration.Configuration
      // .EnableSwagger(c => c.SingleApiVersion("v1", "A title for your API"))      
      // .EnableSwaggerUi();

      HttpConfiguration httpConfig = new HttpConfiguration();
      SwaggerConfig.Register(httpConfig);      
      
      WebApiConfig.Register(httpConfig, unityContainer);
      app.UseWebApi(httpConfig);

// SwaggerConfig.cs...
And in my SwaggerConfig.cs I have...

    	//[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]

		namespace Micromine.MyApp
		{
		  public class SwaggerConfig
		  {
			public static void Register(HttpConfiguration httpConfig)
			{
			  httpConfig
				   .EnableSwagger(c =>
				   {            
                                          // I modified this RootUrl as per FAQ
					   c.RootUrl(req => req.RequestUri.GetLeftPart(UriPartial.Authority) + req.GetRequestContext().VirtualPathRoot.TrimEnd('/'));    
    
					  c.SingleApiVersion("v1", "MyCompany.MyApp");             
					  c.IncludeXmlComments(string.Format(@"{0}\bin\myapp.xml", System.AppDomain.CurrentDomain.BaseDirectory));
				   })
				   .EnableSwaggerUi(c =>
					   {                 
					   });
			}


I have modified the c.RootUrl(req =>req.RequestUri.GetLeftPart(UriPartial.Authority) +req.GetRequestContext().VirtualPathRoot.TrimEnd('/')); as per the FAQ. Before I did this, even when I manually entered thehttp://localhost/myapp/swagger` the routes did not display, as the incorrect url also was entered into the UI "explore" box, however, adding the RootUrl as above fixed this.

The only thing it did NOT fix was the initial navigation to the swagger UI. Note that once I get to the UI, all else seems to work.

EXPECTED RESULT:

Simply some instruction (or otherwise) on how to configure (or work around) getting to the Swagger UI for an asp.net Owin project.

So, when I navigate to http://localhost/myapp/swagger` is redirects to http://localhost/myapp/swagger/ui/index. If solved, would then be good to add this to the FAQ

ACTUAL RESULT:

When I navigate to http://localhost/myapp/swagger is redirects to http://localhost/swagger/ui/index.

@alaniemieckota
Copy link

I have the same problem.

@M0ns1gn0r
Copy link

Yep, same for me.

@GenweiLi
Copy link

GenweiLi commented Jun 11, 2019

Any progress for this issue? I have a similar problem.

@orney21d
Copy link

Any solution for this ?? I'm facing the same issue. Thanks in advance.

@LandryDubus
Copy link

Same problem here.

@number-phi
Copy link

Same problem...

@number-phi
Copy link

I've just resolved my problem removing "RootUrl" from the EnableSwagger, Swashbuckle now create the correct URL for my API hosted in a virtual directory.

@stuartag
Copy link

I had this. Once you set the RootUrl resolver in EnableSwagger as suggested by @pjc2007, it will work, however the old redirect might be cached by your browser - took me a few minutes to work that out.

swagger.RootUrl(resolver =>
    resolver.RequestUri.GetLeftPart(UriPartial.Authority) + resolver.GetRequestContext().VirtualPathRoot.TrimEnd('/'));

@nvaghari
Copy link

same problem

@skironDotNet
Copy link

I'm hacking it with the use of Referrer as UI is loaded by the browser, the referrer is auto send and this makes it work

        app.UseSwagger(options =>
        {
            //Workaround to use the Swagger UI "Try Out" functionality when deployed behind a reverse proxy with routing
            options.PreSerializeFilters.Add((swagger, httpReq) =>
            {
                //The assumption is being made here that index.html is hosted in the root of a virtual directory where a route can be a root
                //example without a proxy
                //https://example.com/index.html <-- the API is directly accesible at domain root like domain.com/v1/users
                //
                //example with a proxy and routing
                //https://example.com/appx/index.html <-- the API is accesible at domain.com/appx "root" like domain.com/appx/v1/users
                //everything should be relative to domain.com/appx
                if (httpReq.Headers.ContainsKey("Referer"))
                {
                    var referer = httpReq.Headers["Referer"].ToString();
                    var index = referer.IndexOf("index.html");
                    if (index >= 0)
                    {
                        var basePath = referer.Remove(index);
                        swagger.Servers = new List<OpenApiServer> { new OpenApiServer { Url = basePath } };
                    }
                }
            });
        });

Would be nice if Swagger was using JS (window.location) to check the location of index.html (or any other start page is possible to configure) and assume that URL is the base url for all requests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants