Skip to content

OwinGlobalAsax

Rico Suter edited this page Aug 19, 2016 · 31 revisions

This page explains how to use the NSwag OWIN middleware in your "Global.asax"-based web project.

Sample project

1. Install NuGet packages

Install the NuGet packages:

  • Microsoft.Owin.Host.SystemWeb
  • NSwag.AspNet.Owin

2. Edit web.config

Then open your Web.config and add the following app setting:

<configuration>
    <appSettings>
        <add key="owin:AutomaticAppStartup" value="false" />
    </appSettings>
    ...

In the system.webServer tag, set runAllManagedModulesForAllRequests to true so that all requests are piped to ASP.NET:

	<system.webServer>
        ...
        <modules runAllManagedModulesForAllRequests="true">
            ...

3. Edit Global.asax.cs

Now, open the Global.asax.cs and add the following call at the beginning of the Application_Start method:

public class WebApiApplication : System.Web.HttpApplication
{
	protected void Application_Start()
	{
		RouteTable.Routes.MapOwinPath("swagger", app =>
		{
			app.UseSwaggerUi(typeof(WebApiApplication).Assembly, new SwaggerUiOwinSettings
			{
				OwinBasePath = "/swagger"
			});
		});
	
		...

Now, start the web project and browse to "http:/localhost:port/swagger" and the Swagger UI should be loaded.