Swashbuckle.WebApi
Swashbuckle.WebApi copied to clipboard
Swagger UI page returns no resource found if implement swashbuckle with Owin in separate class library project
VERSION:
Swashbuckle v5.6.0
STEPS TO REPRODUCE:
-
The current application is asp.net application deployed on IIS.
-
In the Solution, add a Class library project (.net framework 4.6.1, named as WebApi. Create OwinStartUp class: `public void Configuration(IAppBuilder app) { var config = new HttpConfiguration();
SwaggerConfig.Register(config); //config.Services.Replace(typeof(IAssembliesResolver), new WebApiAssemblyResolver()); config.AddApiVersioning(options => { options.AssumeDefaultVersionWhenUnspecified = true; options.DefaultApiVersion = new ApiVersion(new DateTime(2019, 7, 1)); options.ReportApiVersions = true; }); // Web API routes var constraintResolver = new DefaultInlineConstraintResolver() { ConstraintMap = { ["apiVersion"] = typeof( ApiVersionRouteConstraint ) } }; config.MapHttpAttributeRoutes(constraintResolver); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "v{version}/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); config.Formatters.Clear(); config.Formatters.Add(new JsonMediaTypeFormatter()); config.EnsureInitialized(); app.Map(new PathString("/api"), apiApp => { apiApp .UseNancy(options => options.PassThroughWhenStatusCodesAre(HttpStatusCode.NotFound)) .UseWebApi(config); }); }
}`
Nuget installs Swashbuckle. SwaggerConfig.cs: `//[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
namespace Blue.WebApi { public class SwaggerConfig { public static void Register(HttpConfiguration httpConfiguration) { var thisAssembly = typeof(SwaggerConfig).Assembly;
httpConfiguration.EnableSwagger(
c =>
{
c.SingleApiVersion("v1", "WebApi");
})
.EnableSwaggerUi();
}
}
}`
-
In Web.config file of Startup project, add the following setting for Owin:
<appSettings> <add key="owin:AppStartup" value="WebApi.OwinStartup" /> <add key="owin:HandleAllRequests" value="true" /> </appSettings>
-
Now go to one of the api enpoint http://localhost:8083/webservice/api/v1/applicationversion, it looks work well. Go to swagger UI http://localhost:8083/webservice/swagger, it returns a page showing "return resource has been moved.....".
Module | IIS Web Core |
---|---|
MapRequestHandler | |
StaticFile | |
0x80070002 | |
![]() |
EXPECTED RESULT:
Swagger UI should show up.
ACTUAL RESULT:
Swagger UI shows error page.
ADDITIONAL DETAILS
Visual studio 2019
The question is bit old, not sure its had been resolved?. But whoever lands here for the first time, here is my answer.
When you deploy the app in any server with virtual directory, you should set the RouteTemplate
, RoutePrefix
and SwaggerEndpoint
accordingly.
In the above scenario,
The RouteTemplate
should be,
app.UseSwagger(options => {
//Change the path of the end point , should also update UI middle ware for this change
options.RouteTemplate = "webservice/swagger/{documentName}/swagger.json";
});
The RoutePrefix
and SwaggerEndpoint
should be,
app.UseSwaggerUI((options) =>
{
options.RoutePrefix = "webservice/swagger";
options.SwaggerEndpoint("webservice/swagger/v1/swagger.json", "My Api");
});
Where webservice
is your virtual directory.
Hope it helps.
Note: The code is based on core 3.1