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.
</i>--/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 or higher, you must enable wildcard mappings. On IIS 5, create a
"*" mapping to ASP.NET, so that every request gets routed through the ASP.NET engine. 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 RSVP'd.