As Oxite is a way of demonstrating how to build an application using ASP.NET MVC, I thought that I’d take some time to look at how this application uses Routing, Views, and Controllers. To do this, let’s consider the lifecycle of a request to the Oxite application, and then take a look at how the code runs during that process.
The Routing Table
Within an ASP.NET MVC request, everything hangs off of the current Route, so it’s not surprising to learn that the Routing information is one of the first things that is constructed during the request processing. When the OxiteApplication instance (defined in Oxite.Mvc.dll) is started, the routing table is populated with information about what paths are valid and how requests to them should be handled. To see how this routing information is configured, take a look at the OxiteRoutes class in Oxite.dll:
An example of how routing information is added to the application can be seen in the following lines of code:
routes.MapRoute(
"AdminAreasEdit",
"Admin/Areas/{areaID}/Edit",
new { controller = "Admin", action = "AreasEdit" },
new { areaID = new IsGuid() },
controllerNamespaces,
CreateRouteHandler()
);
In this code we see a path being configured – “Admin/Areas/{areaID}/Edit” – with some defaults for the controller and action being set. The {areaID} placeholder is also constrained via the following routing constraint:
new { areaID = new IsGuid() }
To learn more about the concepts for creating Routes, refer to the following relevant posts from the great series by Luis Abreu:
So that’s a lot of information, and Luis has done a great job in putting it together in such a comprehensible manner. So now we understand that, from the routing table, we can map any URL request to Route data. Specifically we know information relating to:
- What Controller class should be used
- What Method (action) on the Controller to use to handle the requwest
- What View to display
In the example code above, these would be:
| Controller class |
controller = "Admin" |
| Controller method |
action = "AreasEdit" |
| View |
"Admin/Areas/{areaID}/Edit" |
The Controller classes can be found in the Oxite.Mvc .dll as follows:
… and indeed, that’s where we can find our “AreasEdit” method:
These methods generally use logic and store data in the ViewData and then return a result by calling View() on the base Controller class:
And this ViewData information is then accessed directly from within the pages that can be found within the OxiteSite/Views folder:
So that’s pretty much how the Oxite application (and ASP.NET MVC applications in general) handle requests. From this post you should now have enough information to know how to wire up new routes and views for doing things such as editing posts, managing users, or whatever other functionality you want to add to your version of the application.
Custom Route Handlers
One final thing to call out, is the use of a custom RouteHandler which is used to handle requests for MetaWeblog API requests. If we cast our eye back to the code at the top of this article, where the Route was added to the routing table for the application, we can see that, the last argument of that method adds the following line of code:
CreateRouteHandler()
In the case of the Oxite application, this method returns an instance of the current default IRouteHandler that is configured to be used for the application. In the case of Oxite, the default IRouteHandler is the in-built default – MvcRouteHandler. This is essentially the IHttpHandler class that will get used to handle the request. For all requests to web pages this is the default, but in the case of the views used to handle MetaWeblog requests, a custom IRouteHandler class is used (XmlRpcRouteHandler):
protected virtual void RegisterMetaweblogRoutes()
{
routes.Add(
"MetaWeblogApi",
new Route(
"MetaWeblogApi",
new RouteValueDictionary(),
new RouteValueDictionary(new { api = new HttpMethodConstraint("POST") }),
new XmlRpcRouteHandler<MetaWeblogService>()
)
);
}
To learn more about creating custom IRouteHandler’s and why you might do so, please refer to another of Luis’ excellent posts about the subject:
The MVC framework – the MvcRouteHandler