ASP.NET MVC from scratch

Yesterday I thought that it would be a good idea to check out ASP.NET MVC (MVC) on my “on-the-edge” rig with Visual Studio 2010 running on Windows 7.

It shouldn’t have surprised me then, that ASP.NET MVC support is currently unavailable for this combination. According to Phil Haack it was something like bad timing that ASP.NET MVC didn’t make it into the Visual Studio Beta. Anyway…

What happens when you try to play with ASP.NET MVC on your machine? Well, you can install ASP.NET MVC which will work alright. MVC is placed in Program Files (without me being able to change the location, GRR…) and into the Global Assembly Cache (which is really not very accessible on the system anymore…). However, the complete Visual Studio Support with Project Template and all is missing. Since all tutorials etc. expect you to be able to just start a new MVC project from the in-built template you are slightly stuck.

My route was to get a not too complex and up-to-date example application. Nerddinner fits this bill quite well. If you have ever developed something with ruby on rails you will feel right at home. When you download and open it up you will stumble over the fact that the project type is unknown to Visual Studio which will stop the project from being loaded. In such cases you usually have to remove the GUID that identifies the type of project. This blog entry here describes the procedure in sufficient detail.

What follows are the necessary bits that were required to get an ASP.NET MVC App running:

void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = "" }
    );
}

protected void Application_Start(object sender, EventArgs e)
{
    RegisterRoutes(RouteTable.Routes);
}
public void Page_Load(object sender, System.EventArgs e) {
    HttpContext.Current.RewritePath(Request.ApplicationPath, false);
    IHttpHandler httpHandler = new MvcHttpHandler();
    httpHandler.ProcessRequest(HttpContext.Current);
}

This apparently ensures that calling the home of a web site kicks off the MVC stuff.

Armed with this my MVC Hello World was up and running without the need for a suitable project template.