On October 6, 2007, Scott Guthrie officially unveiled the ASP.NET MVC Framework at the AltNetConf in Austin, TX. It’s interesting he chose to present it for the first time at this particular venue because the target audience contained some of the most avid open-source software users including users of the MonoRail MVC framework. What could have been perhaps his most hostile and critical audience turned into happy campers after he presented the details and goals of ASP.NET MVC.

Why Model-View-Controller?

ASP.NET has been the odd one in the Web world. While you’ll find several MVC frameworks in the Java space, ASP.NET has provided Web Forms as the default model. Ruby on Rails framework has captured great fanfare thanks in no small part to the core MVC architecture. For .NET developers, MonoRail, to date, has been the only MVC alternative to Web applications. With all the focus on MVC, one might wonder why? Mode-View-Controller is a mature, proven architecture for the presentation layer. It’s applicable in fat client applications as well as Web applications. When applied to Web applications, the twist is that the controller lives for a single HTTP request and then goes away. Fat client controllers are more stateful and live for the life of the screen, responding to user input. With Microsoft’s announcement of the shift of ASP.NET from Web Forms to MVC, buzz abounds on the blogosphere about how great of an improvement this will be for Web applications built on the .NET platform.

What’s Wrong with Web Forms?

Many developers agree that the Postback and ViewState model employed by Web Forms causes pain and heartache. A quick Google search for “ViewState problem” and “Postback problem” brings back 300,000+ and 400,000+ results, respectively. Scott Guthrie reports that a lot of customer have voiced concern about the Web Forms model and had requested an MVC framework to simplify Web application development. In this author’s opinion, the main problem with Web Forms is that it employs an elaborate scheme to hide the developer from the stateless nature of HTTP. With event handlers and the page lifecycle, the developer falls into managing state with ViewState and Postbacks, and when employed in the reality of the stateless Web, the model becomes fragile and has problems scaling with complexity.

Goals of the ASP.NET MVC Framework

The MVC framework started as a prototype created on one of Mr. Guthrie’s airplane rides. It is still in prototype stage with the hopes of reaching CTP status before the end of 2007. Some of the stated goals of the framework are:

  • Enable clean separation of concerns
  • Be testable by default
  • Support Inversion of Control (IoC) containers and third-party view engines
  • Support customization of URLs
  • Leverage existing ASP.NET features
  • Support static and dynamic languages

With a change in architecture, there are a few constraints that are unavoidable, and this author thinks of them as benefits but will be a change for existing applications, nonetheless:

  • Views will no longer use ViewState
  • Views will not use Postbacks or Postback events

Let’s Dig Into Some Code!!

The simplest way to get a Web page up and running on the new MVC framework is to create a route and a class that implements the IController interface. First I’ll show you how to create a route to map http://www.partywithpalermo.com/rsvp to an RsvpController.

--/Global.asax.cs File
    
  public class Global : HttpApplication
  {
    protected void Application_Start(object
       sender, EventArgs e)
    {
      Router.Routes.Add(new Route("rsvp",
         "/rsvp/[action]",
         typeof(RsvpController)));
    }
  }
    
--/Controllers/RsvpController.cs File
    
  public class RsvpController : IController
  {
    public void Execute(HttpContext context,
    RouteData routeData)
    {
      context.Response.Write("<h1>Thanks for
      RSVPing</h1>");
    }
  }

Note that you must have the following set up in Web.config:

<?xml version="1.0"?>
<configuration>
   <system.web>
      <pages>
         <namespaces>
            <add namespace=
             "System.Web.Mvc"/>
         </namespaces>
      </pages>
      <httpModules>
         <add name="ControllerModule"
              type="System.Web.Mvc.
              Handlers.ControllerModule"/>
      </httpModules>
   </system.web>
</configuration>

The ControllerModule class creates a handler for the request; therefore, there is no need to configure the handler in the Web.config file. If you’re running on IIS 6 and up, you must enable wildcard mappings or on IIS 5, create a “.*” mapping to ASP.NET so that every request is routed. By default, IIS will try to serve requests that appear to be at the directory level.

Note that I can match a URL with the controller that should handle it. I’m not required to use a view but to merely implement an Execute method. Now let me take it further and take advantage of the Controller base class. I’ll extend the controller so that it can list all Attendees who have RSVPed.

--/Controllers/RsvpController.cs File
    
  public class RsvpController : Controller
  {
    [ControllerAction]
    public void Index()
    {
      RenderView("RsvpList");
    }
  }

Note that a key change inherits from the Controller base class. This performs the Execute() and wires up actions. By using the ControllerActionAttribute you can declare that the Index() method should be exposed as an action. Index() is the default action. For example’s sake, I’ll change the URL to be http://www.partywithpalermo.com/rsvp/list where list will be the action.

--/Controllers/RsvpController.cs File
    
  public class RsvpController : Controller
  {
    private IAttendeesRepository repository
       = new AttendeeRepository();
    
    [ControllerAction]
    public void List()
    {
      IEnumerable<Attendee> attendees =
         repository.GetAttendees();
      RenderView("RsvpList", attendees);
    }
  }

Note the RenderView() method. “RsvpList” denotes the name of a view in the /Views folder in the Web application. It will look for /Views/RsvpList.aspx, load it, and render it. In order to retrieve the Attendees the code uses IAttendeeRepository. The code sets a property bag called ViewData with the objects that need to go to the view. In this case there’s a single object but you can set several since ViewData is an IDictionary unless overridden.

--/Views/RsvpList.aspx File
    
<%@ Page Language="C#"
   AutoEventWireup="true"
   CodeBehind="RsvpList.aspx.cs"
   Inherits="PartyWithPalermo.Website.Views.
           RsvpHome" %>
<%@ Import namespace="PartyWithPalermo.
           Domain.Model"%>
<%@ Import namespace="PartyWithPalermo.
           Website.Controllers"%>
    
<!DOCTYPE html PUBLIC
          "-//W3C//DTD XHTML 1.0
           Transitional//EN"
          "http://www.w3.org/TR/
           xhtml1/DTD/xhtml1-
           transitional.dtd">
    
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>Party with Palermo</title>
</head>
<body>
  <table border="1">
    <tr>
      <td>Name</td>
      <td>Website</td>
      <td>Comment</td>
    </tr>
    <% foreach (Attendee attendee in
                ViewData) { %>
    <tr>
      <td><%=attendee.Name %></td>
      <td><%=attendee.Website %></td>
      <td><%=attendee.Comment %></td>
    </tr>
    <% } %>
  </table>
</body>
</html>
    
--/Views/RsvpList.aspx.cs File
    
  public partial class RsvpHome :
         ViewPage<IEnumerable<Attendee>>
  {
    protected void Page_Load(object sender,
                             EventArgs e)
    {
    }
  }

The only difference with the code-behind is we inherit from ViewPage instead of merely Page. By declaring the generic form of ViewPage you have strongly-typed ViewData, and you can create a presentation DTO to hold any information that needs to be passed to the view. You’ll get IntelliSense when binding objects to the view.

Table 1 shows the view rendered to the browser.

Note how simplified this view is. This technique allows you to leverage the raw power of HTML (and still use server controls) without ViewState, Postbacks or a server-side form wrapping the entire page.

Conclusion

This brief overview of Microsoft’s new ASP.NET MVC framework barely scratches the surface of its capabilities. I did not cover mapping query string or form variables into controller actions, output caching, etc., but the framework has all these covered. The ASP.NET MVC framework also supports advanced scenarios including Inversion of Control (IoC) container creation of controllers and loading UserControls as a partial view. The framework is still in prototype stage, but when it hits CTP status in a month or so, check out my blog for more information- http://feeds.feedburner.com/jeffreypalermo.