Wednesday, January 21, 2009 11:45 PM
DmitryR
ASP.NET MVC “Plan 9” Sample
[Latest update here]
The attached sample implements simplified Web application paradigm (called “Plan 9 MVC”) based on ASP.NET MVC. It is page-centric (it hides Controllers) but it retains many of the benefits of ASP.NET MVC. The package contains a Visual Studio solution with two projects (the standard ASP.NET MVC starter template rewritten as a Plan 9 Web Site, and the library implementing Plan 9 framework), the license document, and README document.
ASP.NET MVC provides many benefits including:
- Separation of Concerns and the resulting testability
- Better control over HTML in MVC views (compared to classic Web Forms). Rendering using HTML helpers provided for MVC View Pages.
- Nice looking logical URLs
- Productivity features like binding of HTML form fields to action method arguments and declarative authorization rules (by decorating action methods)
Plan 9 completely ignores #1 above, as it does not have Controllers visible to the user. However, the other benefits stay.
The website does not have “Controllers” and “Views” folders – it has “Pages” folder instead, containing ASP.NET pages implemented as MVC View Pages, with HTML helpers. Requests are mapped to these pages using a simple rule: ~/Home/Index is routed to ~/Pages/Home/Index.aspx
The code normally found in Action Methods of a Controller, in Plan 9 applications resides in page methods, Page_Load and the newly introduced (in Plan 9) Page_Post.
Page_Load can be decorated with [Authorize] attribute, similar to how the attribute is used on Action Methods in Controllers:
[Authorize]
public void Page_Load() {
ViewData["Title"] = "Change Password";
}
…
Page_Post method is only executed on HTTP POST requests and its arguments are automatically bound to the request data just as Action Method arguments are:
public void Page_Load() {
ViewData["Title"] = "Login";
}
public void Page_Post(string username, string password, bool rememberMe, string returnUrl) {
…
}
Plan 9 also introduces several helper methods for link generation and validation, to simplify the page code.
Internally Plan 9 implementation is based on ASP.NET MVC. The requests are routed to one controller (all to one Action Method) that dispatches them to the corresponding pages:
public class Plan9Controller : Controller {
public ActionResult Action() {
string virtualPath = Plan9.GetPath(ControllerContext.RouteData);
if (!HostingEnvironment.VirtualPathProvider.FileExists(virtualPath)) {
throw new HttpException(404, string.Empty);
}
Plan9.CurrentControllerContext = ControllerContext;
return View(new WebFormView(virtualPath));
}
}
Obviously, Plan 9 is not a true MVC framework and should not be considered as such. But it does provide a simple alternative way to develop web applications preserving many of the benefits of ASP.NET MVC. In the future it would be interesting to explore a hybrid model, applications that combine true MVC with Plan 9 sub-sections.
The name "Plan 9" refers to Plan 9 from Outer Space (a 1950s zero-budget Sci-Fi movie, some claim it to be the worst movie ever made -- it's so bad it's fun to watch).