ASP.NET MVC provides added flexibility and control.
ASP.NET is a stable, mature, and productive Web development platform, but the 8-year-old framework is showing signs of age. One alternative is MonoRail, a variation of the classic Web Forms model based on views (the HTML output) and controllers (methods executed in response to requests). MonoRail's views are produced by an ad hoc engine that uses a source template and input data to produce the HTML.With MonoRail, you don't focus on pages as you do with Web Forms. Instead, you focus on the actions being taken from the page (methods on a controller class) and its user interface (markup and data placeholders in the view). This leads to separation of concerns (SoC), where the functions of each feature in the program overlap as little as possible, and higher testability.
But you have to abandon ASP.NET to get more control over HTML and SoC. It's possible to produce better ASP.NET Web Forms pages that have an increased level of testability and SoC by handcrafting code-behind classes to place code in a separate file. Nevertheless, the ASP.NET runtime environment isn't designed with extensibility in mind, and the Page Controller pattern that it uses inherently leads to black-box solutions that limit developers' freedom.
Another ASP.NET alternative is the Model-View-Controller pattern. It's used to create the Web Client Software Factory, a collection of reusable components and libraries for building Web clients that rely on the MVC pattern to generate the UI. WCSF comes with Visual Studio templates, automated tests, and wizards for speeding up development. But it's a productivity tool for complex, mostly enterprise-class applications and may not be appropriate for run-of-the-mill Web development. That's why the preferred option is ASP.NET MVC.
ASP.NET MVC At A Glance
ASP.NET MVC 2.0 is a free Web framework that gives you control over your HTML and URLs, facilitates Ajax scripting, and encourages test-driven development. Based on the ASP.NET runtime environment, ASP.NET MVC makes developing Web apps a significantly different experience from using Web Forms. It uses a Web-oriented variation of the MVC pattern, and version 2.0 works with both Visual Studio 2008/.NET 3.5 and Visual Studio 2010/.NET 4.
ASP.NET MVC programs also use controllers and views, but you have to decide how to pass data to the view and expose your middle tier to the controllers. Each request is resolved by invoking a method on a controller class. No postbacks are required to service user requests and no viewstate is required to save the state of the page. Finally, server controls aren't necessary to produce HTML. However, ASP.NET MVC is still based on handling HTTP requests, except that the URL string is treated differently, and any resulting action is expressed by developers using methods on controller classes instead of postbacks.
In ASP.NET MVC, all incoming requests are channeled through a single component--the MVC HTTP handler--in a front controller approach. It contains the logic that parses the URL and decides which controller will service the request. The controller is a plain class with public methods; each method executes a task following a user action and subsequent HTTP request.
The big change in ASP.NET MVC from ASP.NET is that a URL indicates an action to take, not a file to process. Each URL contains routing information for the front controller to identify the target controller. Set in the root directory's global.asax application file, mapping rules are an important part of the application. You organize the application around a few controller classes each with a set of methods. Action and production of the response are distinct steps that distinct subsystems take care of. This provides true separation of concerns because processing logic and generating responses are distinct components.
Improved testability follows from SoC and also from a design change of the MVC HTTP handler. ASP.NET intrinsic objects such as Request, Response, Session, and HttpContext are abstracted to interfaces, and the handler uses them only through the interface. This means that base classes for these objects are available. Full testability is guaranteed and doesn't require you to spin up the whole ASP.NET runtime.
Finally, ASP.NET MVC is neither bound to server controls nor to other rendering technologies. It generates responses through an object that implements a given contract--the IViewEngine interface. The default view en- gine supports the familiar ASPX syntax and server controls. Any view engine you plug in may provide its own syntax for the view and could use any markup syntax. Subsequently, as a developer, you're given all the tools you need to gain the level of control over HTML.
ASP.NET MVC Project Template
The Solution Explorer view of a typical ASP.NET MVC project template makes use of conventions and starts by creating Controller and Views folders:
Under Controllers you find all class files that contain controllers.
Under Views you find one subfolder for each controller. Each controller folder, in turn, contains the source files being used by the selected view engine to generate a response.
A typical project uses the default Web Forms-based view engine and outlines views using ASPX files. Neither index .aspx nor any other similar file is ever requested directly. Those files are simply helper files used by the view engine as the template for the page to render.
ASP.NET Web Forms and ASP.NET MVC pages can be mixed in the same application since they share the same runtime environment. However, ASP.NET MVC apps require a special configuration. For this reason, Web Forms pages can be hosted with ASP.NET MVC pages but not vice versa.
When To Use Which
ASP.NET MVC is a full alternative to Web Forms. But Web Forms isn't on its way out; it will continue to be improved. Choosing between them is a matter of personal preference and customer requirements. If you like Web Forms, then there's no reason you have to change.When making this choice, do consider the skills, education, and attitude of your development team.
If you're not happy with the level of testability and overall design that Web Forms offers, or if you're curious about new technologies, take a look at ASP.NET MVC. It's a completely new framework, designed with SoC and testability in mind. ASP.NET MVC lets you re-experience the way Web development used to be with stateless behavior, full control of HTML, and the freedom to use scripts and cascading style sheets however you want.
6 New Features
1. Asynchronous controllers that increase the scalability of sites that interact with Web services
2. Strongly-typed HTML helpers for generating form input fields and reducing errors caused by typos
3. Client validation support for globalization data for multiple cultures
4. Server validation that renders model-level errors only
5. Templated helpers that automatically associate edit and display elements with data types
6. Html.RenderAction for rendering subsections of a page or site
Read more about Windows development at ddj.com/windows
Dino Esposito is a consultant and author who specializes in Windows development.
Write to us at email@example.com.
Building A Mobile Business MindsetAmong 688 respondents, 46% have deployed mobile apps, with an additional 24% planning to in the next year. Soon all apps will look like mobile apps – and it's past time for those with no plans to get cracking.