Skip to content

OwinGlobalAsax

Rico Suter edited this page Sep 16, 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
			{
				MiddlewareBasePath = "/swagger"
			});
		});
	
		...

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

Read more about settings and options: OwinMiddleware