Setting up a minimal ASP.NET MVC project in Visual Studio

To get started with our minimal project setup we first create a new C# Class Library project and delete the Class1.cs file, the AssemblyInfo.cs file in the Properties folder, and all the references. Now we have a completely empty project to work in.

Project Types

Visual Studio supports different types of projects. The project type determines what functionality is available to you in Visual Studio. If we try to run the Class Library project with Ctrl+F5 we get the following message:

A project with an Output Type of Class Library cannot be started directly.

To change the project type we have to edit the project file. Right-click the project and choose Unload Project and then right-click the project again and choose Edit to open the file in the XML editor. Add the following element to the first property group:


This will not change the project characteristic, since this guid is for the C# Class Library project type (it’s the default type). We can add additional guids to change the project characteristics though. So let’s add the guid for the Web Application project type:


It’s important that you add the Web Application project type guid before the Class Library project type guid, otherwise the project can not be reloaded in Visual Studio. If you reload the project (right-click and choose Reload Project) you will see that the project icon has changed (there’s now a little globe in it). If you hit Ctrl+F5 the built-in ASP.NET Development Server will start and the web application will open in your browser. Also, since the Web Application project type provides extra options, the project file is modified upon reload with an extra section for these options in the ProjectExtensions element.

There is one more thing we need to change in the project: the output path should be bin for all configurations. We can change this in the project properties in Visual Studio: right-click the project, choose Properties, navigate to the Build tab, choose All Configurations and modify the Output path setting.

Setting up routing

In ASP.NET MVC and most other MVC frameworks URLs are not mapped directly to files on disk. Instead, they are mapped to controllers and actions. This mapping is called routing in ASP.NET, and to set it up we need to add a module to the HTTP pipeline that will intercept incoming requests to do the routing.

First we need to add a Web.config file to our project. We will delete everything in this file and then add only what’s necessary. First we add the UrlRoutingModule:

    <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

The UrlRoutingModule hooks into the ASP.NET application life cycle. It will check the URL to see if there is a route that matches the current request. If so, it will set the HttpHandler for the current request to the handler that is specified by the route.

Defining our application

The best way to set up our routes is when the application starts. For this we need to add a Global Application Class to the project (a Global.asax file). One thing that I like to do is to delete the Global.asax.cs codebehind file, create a new file called Application.cs and inherit from this class. Our Global.asax then becomes:

<%@ Application Inherits="Mvc.Application" %>

Defining our routes

By now Visual Studio has already added a few references to the project. We need to add System.Web.Routing to that list as well. This assembly is installed with the .NET 3.5 Service Pack 1. Now we can set up our routes. I prefer to add a special class called Router to set up the routes. We call the RegisterRoutes method on this class from our Application_Start method. Based on a naming convention this method is called once by the HttpApplicationFactory when the application starts. Our application class ends up looking like this:

using System.Web;

namespace Mvc
    public class Application : HttpApplication
        public void Application_Start()

Notice that we pass RouteTable.Routes to our method. This is a static property of type RouteCollection that is the central collection of routes for the application. In our RegisterRoutes method we will construct routes and add them to this collection. A route contains several pieces of information that determine how it maps to a URL and what happens when the route is matched in a request. The UrlRoutingModule will try to match incoming requests to our routes and handle them accordingly. If no route matches, the request is handled as usual. If a route matches the type of RouteHandler for that route is determined. A RouteHandler is expected to provide an HttpHandler that is then set up to handle the request.

I thought this was about ASP.NET MVC?

So far we have only covered setting up an ASP.NET Web Application and setting up our infrastructure for routing. There has been no mention of ASP.NET MVC at all. The interesting thing to notice here then, is that the new routing capabilities of ASP.NET are not exclusive to ASP.NET MVC, they can be used with WebForms too.

We will look at setting ASP.NET MVC in the follow-up post: Setting up a minimal ASP.NET MVC project in Visual Studio, part 2.