Already on Visual Studio 2015 or Visual Studio Code? Check out my blog post on an updated version of this template for VS2015 that uses Yeoman and ASP.NET vNext.
Here is the current situation in Visual Studio 2013:
With the Empty you end up with an empty project. That is, no Web API dependencies included, no OWIN configuration, no basic wiring done.
With MVC or Web API you end up with many dependencies and many things you probably do not need in a simple app. The same applies to the built-in ‘Single Page Application’ template.
In addition, all templates mix the OWIN and pre-OWIN web application structure. Very annoying, because it is just confusing people to have a
Global.asax and a
Startup.cs in a single project and it complicates self host scenarios.
In situations were I introduce my clients to SPA principles these standard templates bring just too much noise and a convoluted project structure.
Instead of using the standard templates, I use a custom template that is inspired by NodeJS. It is based completely on OWIN and Web API 2.2. It uses an OWIN middleware for static file handling and does not rely on IIS at all. This has some nice side effects like being able to declare a specific folder in the project where all my static content (i.e. the SPA) resides.
The structure looks like this:
Some notes on the artifcats:
Startup.cs. The OWIN startup configuration. Configures the public folder (see below) and the WebAPI routes. Attribute routing and a default ‘/api/’ route is configured.
web.config. Basically disables all intercepting IIS features and sets “runAllManagedModulesForAllRequests” to true. The goal is to let OWIN do its job without having IIS features in play. Even static files are served by OWIN.
Controllersfolder. The root folder of the WebAPI controllers. Feel free to change your structure according to your needs.
publicfolder. The root of the file system which is mirrored as static file contents via an OWIN middleware.
public/index.html. The start page for your SPA.
Next steps when working with the template:
- If you plan to use this solution in production, bear in mind that the static file handling of the IIS is disabled and that each request will trigger a managed call. Compared to a native IIS static file handling, the managed pipeline performance is worse.
- ASP.NET vNext will change the game slightly, but this template is supposed to be one of the use cases that gets even better with ASP.NET vNext. The strict OWIN dependency model should make the forward development easier than using a mixed template (MVC and WebAPI).
Download and Usage
You can download the template from the Visual Studio Extension gallery (direct link). After the VSIX installation search for “owin” and select the template:
I have updated the VSIX to version 1.1. It includes a couple of fixes and little improvements:
- New Project GUIDs are now created when activating the template
- A random IIS Express port is now assigned when activating the template
- Removed superfluous NuGet packages and updated JSON.NET to latest version.
I have updated the VSIX to version 1.2. Again, there are some fixes and improvements:
- Fixed an issue with the NuGet hint path that occurred when you de-selected “Create directory for solution”. Thanks to Kim for reporting this!
- Made the template ready to use in offline scenarios by adding the NuGet packages to the VSIX.