Even though ScottGu and others have written about ASP.NET MVC I figured I’d add a series of notations about URL Routing, Controllers and Controller Actions, Rendering Helpers and Understanding the Model.
Who knows maybe the way I explain it will drive the point home.
Part 1 is just my overview of the ASP.NET MVC Framework
ASP.NET MVC Framework (Part 2): Understanding URL Routing
Background
WebForms
In ASP.NET Forms we used Server controls that must appear between a <form> tag. Each server control had to use a runat=”server” attribute to tell the server to process that form on the server.
ViewState
In classic ASP when forms were submitted all form values were cleared. Unfortunately if the form had an error then you lost all that information you submitted. In ASP.NET the form is able to return with all the form values. ViewState is a way for the server to know the status of the page.
Event Driven Programming
ASP.NET objects (server controls) on the webpage exposed events that allowed a programmer to process ASP.NET code. By having a Load, Click or Change event handled by code made coding much simpler and much better organized.
ASP.NET 2.0
In ASP.NET 2.0 we received Master Pages, Themes and Web Parts, more server controls for navigation or security. The provider patterns for Roles, personalization or memberships, etc .
MVC Execution Process
With ASP.NET MVC Framework a web request now passes to an UrlRoutingModule object (HTTP module) which is then parses and a route is selected. Then an MvcHandler take the object and selects a controller instance that will call that controllers Execute method.
Stages of the Mvc Web Project
Initial request
In Global.asax file, routes are added to the RouteTable object
Routing
The UrlRoutingModule creates the RouteData object. RouteData is used to determine which controller to request and which action to invoke
Map to Controller
The MvcRouteHandler handler attempts to create the type name for the controller, based on data in the RouteData instance
Invoke ControllerBuilder
The handler calls the global static CreateController method of the ControllerBuilder class, obtaining an IController instance.
Create Controller
The ControllerBuilder instance creates a new controller directly, or uses a IControllerFactory object to create the controller
Execute Controller
The MvcHandler instance is added to the context and calls the controller’s Execute method.
So instead of Event Handlers we now have Controller Classes and instead of ViewState exposing certain sequences of events for programming, we now have full control over the behavior of an application.
Default Naming Conventions
Pretty simple, if you have a controller name it UrlPathController this is so the UrlRoutingModule and MvcHandler can determine which controllers to invoke. Any controller class has to implement the System.Web.MVC.IController. By implementing this IController you will gain access to using the [ControllerAction] Attribute, which we’ll get into next writing.
Mapping URLs to the Controllers
MVC Framework has a default URL convention it follows to map URL’s. The syntax is:
URL = [Controller] / [Action] / [id]
This means your URLs will look like:
NOTE: If you see a controller-name.mvc this is required for anybody running IIS 6.0 as their web server
Global.asax
This file is used to define route mappings. You do this in the Application_Start event. The syntax is:
// Note: Change Url= to Url="[controller].mvc/[action]/[id]"
// to enable automatic support on IIS 6.0.
RouteTable.Routes.Add(new Route
{
Url = "[controller]/[action]/[id]",
Defaults = new { action = "Index", id = (string)null },
RouteHandler = typeof(MvcRouteHandler)
});
Url = "Default.aspx",
Defaults = new { controller = "Home", action = "Index",
id = (string)null },
This is saying Add a route to the RouteTable which lists my controller and action. Notice you are not required to give it a parameter or id initially.
Here are some example URLs to drive the point home:
URL
RouteData object values
/domain/site/blog
Controller=”blog”, Action=”index”, id=null
/domain/site/blog/ShowEntry
Controller=”blog”, Action=”ShowEntry”, id=null
/domain/site/blog/ShowEntry/20
Controller=”blog”, Action=”ShowEntry”, id=”123”
Next blog we’ll get into the Controllers and Controllers Actions.
Remember Me
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, sup, u
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.