ASP.NET MVC: Another Way to Create Web Pages?
Web developers have always had a harder time than desktop developers. The standards behind Web pages have been – until relatively recently – supported differently by different browsers. The standards themselves change, and the tools used to create Web pages are often little more than text editors. ASP.NET changed much of that: providing a rich, desktop-like programming model that mirrored what many developers were used to developing towards. ASP.NET has been in use for over seven years now, and it's still going strong. Version 2.0 added a number of useful features: membership, profiles, even end-user personalization. Building scalable, feature-rich Web sites is easy with ASP.NET.
As is often the case, some developers were not satisfied with the internal architecture used by ASP.NET. They complained about ViewState, the Page life cycle, and the limited ability to control the generated HTML. Developers using development methodologies that included test-first found testing their Web pages difficult. To solve many of these issues, the ASP.NET development team created an alternate way of building Web pages: the ASP.NET MVC framework. This framework does not replace the existing development framework, and most developers will not need to use it. However, this new way of building Web sites can be incredibly useful in certain scenarios.
The MVC in ASP.NET MVC stands for Model-View-Controller. These three items are the core types of classes that interact with one another within the application. The Model is the data manipulated by the application. By itself, it does little: it should be able to contain and save the data, and perform similar interactions. However, it 'knows' nothing about the Web, or Web pages. The Model may use any available means to store the data: DataSet, Entity Framework, SubSonic, nHibernate, etc. The View is the actual Web page. There may be some code in this page, particularly to enable displaying data, but generally views are simple content pages. Finally, the Controller works to provide the interaction for the application: selecting the appropriate Model, retrieving the data and passing this data to the correct View. The typical application created using ASP.NET MVC will have many of each of these components, each providing access to different objects managed by the system.
The structure of an MVC project is more controlled compared to a regular ASP.NET project (see image to the right). The Model, View and Controller folders separate out the various components. While this structure is not absolutely necessary, it helps to organize the project and simplify the interaction between the components. In addition to this coarse layout, the individual Views are typically organized based on the controllers used, as well as a set that are shared across multiple controllers.
In addition to the model, view and controller components, the global.asax file is used to define the routes used by the site. These routes are a series of expressions that define how a given request maps to the available controllers. Given the following request and the standard set of routes:
The route will map the request to the edit method of the product controller, passing in an id value of 10 to the first parameter. This highlights how simple the URLs used by the MVC system can be: the aspx extension is not required, and parameters are natural parts of the URL. By organizing the mapping using a series of tokens, the framework can call specific methods exposed by the controllers, and data can be passed in to the methods.
When you create a new Web project using ASP.NET MVC, you are prompted to also create a unit test project. By default, this uses Visual Studio Test as the test engine. However, the system is extensible, and many of the third party test engines (such as nUnit, xUnit.net and MbUnit) have already enabled using their format to create tests for MVC applications. As with other applications, using unit testing with ASP.NET MVC increases the overall quality of the application. As each of the three components are independent, each can be tested in isolation. You can also use mocking systems (such as Moq, TypeMock or Rhino Mocks) to further isolate these classes, enabling full testing of your applications without requiring the use of IIS or Web browsers.
ASP.NET MVC is currently available as a technology preview and is still evolving based on customer feedback. There have already been many changes to the base components offered by the framework, and likely many more before it is finally declared "version 1.0". The product team have been very transparent, announcing upcoming changes and interacting with developers to fine tune the technology before release. They have even taken a rare step for Microsoft; they have released the full source code for the framework (as well as all unit tests), and there is talk that they will make incremental builds available on a frequent basis. Even if you are only exploring the technology for possible future use, it's worth downloading the code to see how groups at Microsoft arrange their code and design their tests.
Is ASP.NET MVC ready for real-world use? There have already been a few sites deployed using this new framework. See the references list below for some of them. As the framework is still evolving, you may need to make changes as time goes on, but the core functioning of the framework is solid enough to support building a site on. Switching to development using MVC does take changing the way you build Web pages. For example: gone are many of the drag-and-drop niceties some developers enjoy. However, using ASP.NET MVC fits better into into a test-first development model, and it provides additional flexibility in terms of the structure of the Web site and HTML generated than the traditional WebForm model. It's not for everyone or every application, but ASP.NET MVC is definitely worth looking into if you've been frustrated by the existing ASP.NET model
- ASP.NET MVC – Home page for the ASP.NET MVC framework. Contains information on the framework, quickstart videos and documentation.
- ASP.NET MVC download – Source code for the ASP.NET MVC framework.
- MVC Contributions – Community contributed extensions to the MVC framework. These include additional controller and view base classes, and extensions to integrate with other frameworks, such as The Castle Project.
- Kigg – Digg-clone written using the MVC framework.
- CodeCampServer – Sample MVC application designed for organizing events (specifically Code Camps)