When I installed the ASP.NET MVC Beta, I was disappointed that it only included an ASP.NET Web Application template. Some developers could probably argue for hours over which is better, the Web Application template or Web Site template, but this is more of a personal preference, therefore I won’t be going over the pros and cons of those project types here.
I wanted a Web Site template for ASP.NET MVC, and here I’ll explain how I created it.
The first, and most obvious step, is to install the ASP.NET MVC Beta. Once that’s installed, fire up Visual Studio 2008 and create a new Web Site. Once the project is up and running, add references to the following assemblies:
System.Web.AbstractionsSystem.Web.MvcSystem.Web.Routing
In the default install of the MVC Beta, these assembles are all found in %ProgramFiles%\Microsoft ASP.NET\ASP.NET MVC Beta\Assemblies.
Now it’s time to make modifications to the Web.config to add the HTTP Modules, Handlers, and namespaces for each WebForm/ViewPage.
Under the system.web\pages section of the Web.config, add a node called namespaces and then add the following namespaces:
System.Web.MvcSystem.Web.Mvc.AjaxSystem.Web.Mvc.HtmlSystem.Web.Routing
The XML should look like this:
<!-- ... -->
<namespaces>
<add namespace="System.Web.Mvc">
<add namespace="System.Web.Mvc.Ajax">
<add namespace="System.Web.Mvc.Html">
<add namespace="System.Web.Routing">
</add>
<!-- ... -->
In the section below pages, (it should be httpHandlers), we need to add an entry for the MvcHttpHandler.
<add verb="*" path="*.mvc" validate="false" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
Then add the UrlRoutingModule to the httpModules configuration section.
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
If you plan on deploying to IIS 7.0, then we need to make a few changes to the system.webServer section. Under modules, add the following:
<remove name="UrlRoutingModule">
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
In handlers, add this:
<remove name="MvcHttpHandler">
<add name="MvcHttpHandler" precondition="integratedMode" verb="*" path="*.mvc" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<remove name="UrlRoutingHandler">
<add name="UrlRoutingHandler" precondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
Now we can move on to setting up the project template files. First, add a Global application class (Global.asax). Clear its content below the Application tag. Then make the Global.asax inherit from a class called MvcApplication. (This class doesn’t exist yet, but we will create it soon).
<%@ Application Language="C#" Inherits="MvcApplication" %>
To create the MvcApplication, add a new class file to the App_Code folder. If App_Code doesn’t exist, create it by right-clicking on your project in the Solution Explorer and selecting App_Code from the Add ASP.NET Folder submenu.
The MvcApplication class needs to inherit from HttpApplication. The goal of this class is to register the default route for the default MVC view. Be sure to add the System.Web.Routing namespace to the top of this class file.
using System;
using System.Web;
using System.Web.Routing;
using System.Web.Mvc;
public class MvcApplication : HttpApplication
{
static void Application_Start()
{
// do not route requests for web resources
RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// add the default route
RouteTable.Routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Default", action = "Index", id = "" }
);
}
}
Now that we have the MvcApplication set up, we need to set up the Default web form so it routes requests to the MvcHttpHandler. Here’s the code:
using System;
using System.Web;
using System.Web.Mvc;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HttpContext.Current.RewritePath(Request.ApplicationPath);
IHttpHandler handler = new MvcHttpHandler();
handler.ProcessRequest(HttpContext.Current);
}
}
Now, create two folders within App_Code called Controllers and Models. Then create a folder in your web site’s root called Views. Within the Views folder add a subfolder called Default. In the Controllers folder, create a class file called DefaultController. The source for DefaultController is below:
using System;
using System.Web.Mvc;
[HandleError]
public class DefaultController : Controller
{
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
}
}
This template isn’t going to use Models, that’s for future use though. So let’s skip ahead to adding the default view. In the Views\Default directory add a new Web Form called Index.aspx. Yes, I said Web Form because the ASP.NET MVC Beta also does not include the ViewPage item template for Web Site projects. So the first order of business is to change the codebehind to have the Index view inherit from System.Web.Mvc.ViewPage rather than from System.Web.UI.Page.
public partial class Views_Default_Index : System.Web.Mvc.ViewPage
{
}
Now, go to the Index ViewPage’s markup and add the following line to the body between the div tags:
<%= Html.Encode(ViewData["Message"]) %>
At this point the project can be compiled and debugged. You should see your message (set in the controller) in your default browser.
To save the template, select Export Template from the File menu and follow the steps in the wizard.