Hope this helps, works for me:
clone https://github.com/NSwag/Samples
open solution, I used VS2015 update 3
create a new Owin MVC project
add new "ASP.Net web applation (.Net Framework)" project to solution; I called it: SampleOwinWebApiWithSwaggerUi ... use the default MVC project template (MVC checkbox is checked), also check the "Web API" checkbox.
Change authentication, select no authentication, click OK.
Right click the new project and select "Set as Startup Project"
add nuget package ...
right click the new SampleOwinWebApiWithSwaggerUi project, click Manage NuGet packages. Add this package:
Microsoft.AspNet.WebApi.Owin (adds other dependencies including Owin + Microsoft.Owin + Microsoft.AspNet.WebApi.Owin for you too)
add a web api controller ... I re-used the PersonController.cs and it's associated model Persons.cs from the SampleWebApiWithSwaggerUi project, added controller to a new folder ./api under ./Controllers and just updated namespaces and using statements from 'SampleWebApiWithSwaggerUi' to 'SampleOwinWebApiWithSwaggerUi'
Added an Owin startup.cs file, right-click SampleOwinWebApiWithSwaggerUi project, add new, enter 'owin' in the search box, select OWIN Startup class, change the default name from Startup1.cs to Startup.cs, click Add
Add 'NSwag.AspNet.Owin' NuGet package ... again this adds other packages and updates, as required.
Add 'Microsoft.Owin.Host.SystemWeb' NuGet package
update Startup.cs, adding the following lines:
// from https://github.com/NSwag/NSwag/wiki/Middlewares
var config = new HttpConfiguration();
app.UseSwaggerUi(typeof(Startup).Assembly, new SwaggerUiOwinSettings());
app.UseWebApi(config);
config.MapHttpAttributeRoutes();
config.EnsureInitialized();
as you add these, you will need to add the following dependencies to usings:
using System.Web.Http;
using NSwag.AspNet.Owin;
- update web.config, section
I changed from
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
to:
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<clear />
<add name="Owin" verb="" path="*" type="Microsoft.Owin.Host.SystemWeb.OwinHttpHandler, Microsoft.Owin.Host.SystemWeb" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<add name="SwaggerIndex" path="swagger/index.html" verb="*" type="NSwag.AspNet.Owin.SwaggerUiIndexMiddleware" />
<add name="SwaggerGenerator" path="swagger/v1/swagger.json" verb="*" type="NSwag.AspNet.Owin.SwaggerMiddleware" />
<add name="SwaggerRedirect" path="swagger" verb="*" type="NSwag.AspNet.Owin.RedirectMiddleware" />
</handlers>
</system.webServer>
NOTE: I did not follow advice here https://github.com/NSwag/NSwag/wiki/OwinGlobalAsax as this failed for me:
<appSettings>
<add key="owin:AutomaticAppStartup" value="false" />
</appSettings>
since we do want the Owin startup.cs to load.
- At this point you should be able to use /swagger, web api data ok, however static files such as bootstrap.css are not served for MVC pages. To fix this I followed advice here: https://stackoverflow.com/a/36297940/445927 though I changed the physicalFileSystem root, as below. This additional code should be added to the end of startup.cs, after swagger additions noted above.
// to serve static files, see: https://stackoverflow.com/a/36297940/445927
string root = AppDomain.CurrentDomain.BaseDirectory;
//var physicalFileSystem = new PhysicalFileSystem(Path.Combine(root, "wwwroot"));
var physicalFileSystem = new PhysicalFileSystem(root));
var options = new FileServerOptions
{
RequestPath = PathString.Empty,
EnableDefaultFiles = true,
FileSystem = physicalFileSystem
};
options.StaticFileOptions.FileSystem = physicalFileSystem;
options.StaticFileOptions.ServeUnknownFileTypes = false;
app.UseFileServer(options);
Finally you should have ASP.Net MVC (with static files), Web API and Swagger all working.
Update: Rico has now merged into main repo: https://github.com/NSwag/Samples