Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [14]

By Root 5466 0
brings to the table a clean design with a neat separation of concerns, a leaner run-time stack, full control over HTML, an unparalleled level of extensibility, and a working environment that enables, not penalizes, test-driven development (TDD).

ASP.NET Web Forms and ASP.NET MVC applications can go hand in hand and live side by side in the same process space. The runtime environment must be configured to host an ASP.NET MVC application. This means installing a routing module that intercepts requests at the gate and decides how they are to be processed. An ASP.NET MVC application lists one or more URL patterns it will accept. Requests whose URL matches any defined patterns are processed as ASP.NET MVC requests, while others are left to the standard processing engine of Web Forms.

Control over Markup


Just like with Web Forms, what some perceive as a clear strength of ASP.NET MVC, others may see as a weakness. ASP.NET MVC doesn’t offer server controls of its own and also severely limits the use of classic ASP.NET server controls. Even though you describe the view of an ASP.NET MVC page via ASPX markup, you can’t embed in it server controls that handle postbacks. In other words, you are allowed to use a DataGrid if your goal is creating a table of records, but your code will receive an exception if the DataGrid is configured to allow paging, sorting, or inline editing.

To gain full control over HTML, JavaScript, and CSS, ASP.NET MVC requires that you write Web elements manually, one byte after the next. This means that, for the most part, you are responsible for writing every single

  • or tag you need. In ASP.NET MVC, there’s no sort of component model to help you with the generation of HTML. As of today, HTML helpers and perhaps user controls are the only tools you can leverage to write HTML more quickly. Overall, some developers might see ASP.NET MVC as taking a whole step backward in terms of usability and productivity.

    Adding visual components to ASP.NET MVC is not impossible per se; it is just arguably what most users of the framework really want. My opinion is that keeping any form of markup abstraction far away from ASP.NET MVC is OK as long as you intend to have two distinct frameworks for ASP.NET development. But I do hope that we move soon to a new framework that unifies the Web Forms and ASP.NET MVC models. In this new framework, if it ever arrives, I do expect some markup abstraction as the only way to increase productivity and have people move to it.

    ASP.NET MVC and Simplicity


    Simplicity is a characteristic that is often associated with ASP.NET MVC. If you look at Figure 1-8, you can hardly contest the point—ASP.NET MVC is architecturally simpler than Web Forms because the sequence of steps to process a request follows closely the rules of the underlying protocols, with no abstractions created by the framework.

    This is a correct statement, but it is a bit incomplete. ASP.NET MVC processes a request through an action and passes return values to a view engine. In doing so, though, ASP.NET MVC offers a number of free services that you might or might not need. For example, when a form posts its content, the framework attempts to bind posted data to the formal parameters of the action method in charge of serving the request. There’s a lot of reflection involved in this approach, and some work is done that might not strictly be needed. Can you opt out of this model binding, and how easy is it to do so?

    This is the point that shows why ASP.NET MVC targets simplicity in a much more effective way than Web Forms.

    In ASP.NET MVC, opting out of a built-in feature simply requires that you use a different coding convention. There’s nothing to disable and no closure to crack open to get a different behavior. Any complexity in ASP.NET MVC is built in a bottom-up manner, by composing layers of code one on top of the other. At any time, you can step back and remove the topmost layer to replace it or simply do without it.

    In Web Forms, opting out of any built-in feature is much harder because the framework

    ®Online Book Reader