Getting Swagger to work from .NET OWIN

If you are implementing an API using .NET WEB API 2 & OWIN, and want to leverage swaggerUI to visualize your API metadata, then beware of this issue! Recently I integrated SwaggerUI with my API project following a method similar to this. I faced an issue where SwaggerUI would work perfectly fine during local testing of the API, but would fail to work on production deployments of the API. Since the API is hosted via OWIN the virtual path will not remain the same between different enviroments. This is due to have OWIN works internally.

 

 To solve this issue you have to resolve the correct virtual path upon enabling swagger as shown below: 

 

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

} )

 

 

Thats it! YIPPEE for one liners!! You should be able to access your SwaggerUI on all environments in the same manner. 

 

This article is a good reference point.

 

Subscribe For Instant News