ASP.NET 5 Middleware Pipeline and Startup Class

ASP.NET 5 Middleware Pipeline and Startup Class

I am really enjoying the radical new architecture in ASP.NET 5 with its middleware pipeline. An empty ASP.NET 5 web project starts out very lightweight and then you add middleware into the request pipeline similar to how we added HttpModules in previous versions of ASP.NET. Nothing demonstrates this new lightweight, modular approach more than the fact that in an empty ASP.NET 5 Web Application there isn't support for static files, default documents, and directory browsing, which are all pretty basic website features ( even if you don't need them ). In this ASP.NET 5 tutorial I want to briefly look at the new Startup Class and how to add middleware to your ASP.NET 5 web applications. In future tutorials I will build custom middleware and go much more in depth.

ASP.NET 5 Startup Class

Although I didn't mention the Startup Class specifically, I used the Startup Class' ConfigureServices Method in two tutorials discussing dependency injection in ASP.NET 5 and ASP.NET MVC 6: ASP.NET MVC 6 Dependency Injection and ASP.NET MVC 6 View Injection. The Startup Class in ASP.NET 5 also has a Configure Method that allows one to add middleware to the HTTP request pipeline. In the code below I am adding new middleware in ASP.NET 5 to enable the use of static files, default documents, and directory browsing. Most websites will need static files, but default documents and directory browsing are typically not used. In the past, however, all of this was enabled pretty much by default, and now in ASP.NET 5 you have to explicitly add the middleware.

public class Startup {
    public void Configure(IApplicationBuilder app) {
        app.UseIISPlatformHandler();

        app.UseStaticFiles();
        app.UseDefaultFiles();
        app.UseDirectoryBrowser();
    }
}

This is a radical change in ASP.NET 5. This functionality is not available by default. We had to add middleware above to the HTTP request pipeline to enable it.

Project.json in ASP.NET 5

If you make the change above, you will notice the ASP.NET Web Application still doesn't compile. There is still more to do. In addition to the above, you also need to modify the project.json configuration file and add a dependency on Microsoft.AspNet.StaticFiles.

"dependencies": {
  "Microsoft.AspNet.IISPlatformHandler": "1.0.0-beta8",
  "Microsoft.AspNet.Server.Kestrel": "1.0.0-beta8",
  "Microsoft.AspNet.Diagnostics": "1.0.0-beta8",
  "Microsoft.AspNet.StaticFiles": "1.0.0-beta8"
}

Now we're done, but as you can see, we had to add both configuration and dependencies just to get some basic functionality.

Conclusion

Quite an interesting set of changes just for static files, default documents, and directory browsing for your ASP.NET 5 web application. This just goes to show how serious Microsoft is about keeping the request pipeline clean and lightweight and giving you the control of what features and functionality you add to your ASP.NET Websites.