Swashbuckle.webapi: OWIN within IIS issue

Created on 11 Mar 2015  路  10Comments  路  Source: domaindrivendev/Swashbuckle.WebApi

I have Web API running with OWIN but within IIS and that's within IIS sub application inside a certain website.

When navigating to localhost/sitename/appname/swagger I encounter an issue saying "Can't read swagger JSON from http://localhost:80/swagger/docs/v1".

The problem is there's part "sitename/appname" missing from the JSON URL. If I manually add it into the textbox like so http://localhost:80/sitename/appname/swagger/docs/v1 the API docs show up.

So I guess they are there and SB finds them it's just that it has problems finding out the correct app URL.

Would this help and where does this go?

SwaggerSpecConfig.Customize(c =>
{
c.ResolveBasePathUsing(req =>
req.RequestUri.GetLeftPart(UriPartial.Authority) +
req.GetRequestContext().VirtualPathRoot.TrimEnd('/'));
}

I can't find a reference to SwaggerSpecConfig..

Most helpful comment

It's on your SwaggerDocsConfig instance. Full code snippet is:

c# configuration.EnableSwagger(c => { c.RootUrl(req => new Uri(req.RequestUri, req.GetRequestContext().VirtualPathRoot).ToString()); })

All 10 comments

I have the same issue when hosting a web application in IIS with OWIN, in Swashbuckle 5.0. I have partially managed to solve the issue by using the following code in the Swashbuckle-configuration:

``` c#
using System.Net.Http;

/* Snip */

config.RootUrl(req => new Uri(req.RequestUri, req.GetRequestContext().VirtualPathRoot).ToString());
```

This at least means that I get the correct URL in the URL-textbox, when accessing the documentation directly at /MyService/swagger/ui/index. However, I still have an issue where accessing the path "MyService/swagger" redirects me to "/swagger/ui/index" and not "/MyService/swagger/ui/index".

I have the same problem on self hosted OWIN.

I did some further investigation into this, and I discovered that the code I posted actually makes Swashbuckle redirect the UI to the proper URL. The problem I was having was that Swashbuckle rightfully returned a 301 Moved Permanently which my browser cached. I cleared my cache and now I am redirected to /MyService/swagger/ui/index when accessing /MyService/swagger.

Snippet from @allansson should be the right fix

@allansson where did you find this config.RootUrl extension? I added the using System.Net.Http and it's not there.

It's on your SwaggerDocsConfig instance. Full code snippet is:

c# configuration.EnableSwagger(c => { c.RootUrl(req => new Uri(req.RequestUri, req.GetRequestContext().VirtualPathRoot).ToString()); })

I can confirm that @allansson fix works with the https://visualstudiogallery.msdn.microsoft.com/e376f2f7-4412-4b75-8ccf-818c45adafe5?SRC=VSIDE template.
My setup in IIS: Basic IIS site at top level, underneath several "Applications" with the above template.

Thank you, @allansson , it helped me!

This issue appears to be closed with just a workaround fix. Isn't there anything that can be changed within SwashBuckle to prevent this problem in the first place?

@mario-d-s
remember this is an OpenSource project...
You can fork it, make any change you see fit, and submit a Pull request.

Was this page helpful?
0 / 5 - 0 ratings