New Features in ASP.NET MVC 2.0

Introduction

Microsoft's ASP.NET is a language and platform-neutral technology, arguably, one of the most successful web technologies ever. You can use it to design and develop web applications that can run on top of the managed environment of .NET Framework and inside the context of the IIS web server. Microsoft's MVC Framework is based on the time-tested and proven MVC Design Pattern and provides you a framework for building scalable, robust, high-performance web applications seamlessly. Applications designed using the MVC Design Pattern is easier to test and maintain. This article takes a look at the new features in ASP.NET MVC 2.0 and how one can leverage them in applications.

Getting Started with ASP.NET MVC 2.0

To work with ASP.NET MVC 2.0, you need to install it separately if you are using Microsoft Visual Studio 2008. Alternatively, you can use any one of the following versions of Microsoft Visual Studio 2010 - ASP.NET MVC Framework 2.0 is built-in.

  • Microsoft Visual Studio 2010 RC
  • Microsoft Visual Studio 2010 Professional or Ultimate

You can download a copy of Microsoft Visual Studio 2010 from this link: http://msdn.microsoft.com/en-us/vstudio and a copy of ASP.NET MVC 2.0 from thi link: http://www.microsoft.com/downloads/details.aspx?FamilyID=c9ba1fe1-3ba8-439a-9e21-def90a8615a9&displaylang=en.

Note that ASP.NET MVC Framework can be installed side by side with your ASP.NET MVC 1.0 and you can easily upgrade your ASP.NET MVC 1.0 applications to ASP.NET MVC 2.0 counterparts with ease. Moreover, Visual Studio 2010 has an built-in upgrade wizard that you can use to migrate your existing ASP.NET MVC 1.x applications to ASP.NET MVC 2.0 counterparts seamlessly. To migrate your old ASP.NET MVC 1.0 applications to ASP.NET MVC 2.0 applications manually, you should replace all occurences of "System.Web.Mvc, Version=1.0.0.0" with "System.Web.Mvc,Version=2.0.0.0", replace the old assemblies with newer ones, and also use bindingRedirect element in the web.config file of your ASP.NET MVC 1.0 application as shown below:

<runtime>
<assemblyBinding xmlns=
"urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc"
publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0"
newVersion="2.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>

Looking Back in Time

ASP.NET MVC Framework 1.0 was released as an external component to Microsoft Visual Studio 2008. The Community Technology Preview of ASP.NET MVC was launched in December 2007. ASP.NET MVC 1.0 was released in March 2009 and ASP.NET MVC 2.0 RTM was released in March 2010.

Understanding the Model View Controller Design Pattern

The Model View Controller (or MVC as it is commonly called), is a proven design pattern that facilitates testability and easier maintenance of the application's code. It also promotes a cleaner separation of the application's concerns and is primarily based on the following major architectural components:

  • The Model - the component that is responsible for storing the application's data and business logic components
  • The View - the component responsible for invalidating the view based on the model's state and presenting the data to the user in the user interface
  • The Controller - the component responsible for managing the interaction amongst these components

The primary advantage of this design pattern is easier maintenance, reduced cost and a cleaner isolation of the application's concerns.

What is the ASP.NET MVC Framework?

The ASP.NET MVC Framework is a framework from Microsoft that can be used to design and implement applications based on the MVC Design Pattern. The basic advantages of using the ASP.NET MVC Framework include: a cleaner separation of concerns, better code organization, extensibility, scalability and code reusability. It also provides an excellent support for a REST-based model and all existing ASP.NET features. As the ASP.NET MVC framework is built on top of the ASP.NET runtime, you can leverage the existing ASP.NET features like authentication and authorization, profile settings, localization, and so on.

Scott Guthrie states in his blog: "One of the benefits of using an MVC methodology is that it helps enforce a clean separation of concerns between the models, views and controllers within an application. Maintaining a clean separation of concerns makes the testing of applications much easier, since the contract between different application components are more clearly defined and articulated."

Reference: http://weblogs.asp.net/scottgu/archive/2007/10/14/aspnet-mvc-framework.aspx



New Features in ASP.NET MVC 2.0

New Features in ASP.NET MVC Framework 2.0

ASP.NET MVC 2 is the new version of ASP.NET MVC Framework - a framework for developing highly testable and maintainable Web applications by leveraging the Model-View-Controller (MVC) design pattern. Here is the list of the new features and enhancements in ASP.NET MVC Framework 2.0:

  • Support for templated helpers - This is a new feature that you can use to associate templates with views for editing and displaying values based on a particular data type.
  • Support for Asynchronous Controllers - You can now use controllers in ASP.NET MVC 2.0 applications to process requests asynchronously.

    The abstract class AsyncController in the System.Web.Mvc namespace provides support for asynchronous operations for controller classes in ASP.NET MVC 2.0. Here is how this class looks like:

    public abstract class AsyncController : Controller, 
    	IAsyncManagerContainer, IAsyncController, IController
    

    The AsyncManager in the System.Web.Mvc.Async namespace provides support for asynchronous operations to the AsyncController class.

  • Support for areas - You can use this new feature to partition and group functionalities in your ASP.NET MVC projects. This is a great new feature introduced in ASP.NET MVC 2.0. Areas enable you to organize ASP.NET MVC Applications to manage the complexity of large web applications seamlessly. In essence, an area is used to represent a section of a web site that is comprised of a group or a collection of related controllers, views, routes and sections. When you create a new area, Microsoft Visual Studio automatically adds a class that derives from AreaRegistration class to register the area and its routes. Here is an example:

    namespace Test.Areas.Employee 
       {
        public class EmployeeAreaRegistration : AreaRegistration 
         {
            public override string AreaName 
            {
                get { return "employee"; }
            }
    
            public override void RegisterArea(AreaRegistrationContext context)
            {
                context.MapRoute(
                    "employee_default",
                    "employee/{controller}/{action}/{id}",
                    new { action = "Index", id = UrlParameter.Optional }
                );
    
                context.MapRoute(
                    "employee_whatsnew",
                    "whats-new",
                    new { action = "WhatsNew", id = UrlParameter.Optional  }
                );
            }
        }
    }
    

  • Support for data annotations - You can use this feature to easily attach validation rules to the Model and View Model classes in your ASP.NET MVC applications.
  • Support for strongly typed UI helpers - You can use this new feature to perform a compile-time check of the views. This feature also facilitates an enhanced Intellisense support for your view templates in an ASP.NET MVC application.
  • Support for Templates and Templated Helpers - The introduction of templates is a major new feature in ASP.NET MVC Framework 2.0. Similar to Dynamic Data for WebForms, this is a feature that allows you to display or edit an object of a simple data or complex type. You can have the following types of templates in ASP.NET MVC Framework 2.0:

    Default Template
    Custom Object Template
    Master Page Template

  • You can use Templated Helpers to create display and edit templates for various types. Templated Helpers enable you to have a compile time check and provide support for intellisense and code refactoring.

  • Support for Client-Side Validation -

    ASP.NET MVC 2.0 provides excellent support for client side validation. You can use attributes from the System.ComponentModel.DataAnnotations namespace. An an example, you can make a property or field mandatory using the [Required] attribute as shown in the code snippet below:

    [Required] 
    public string FirstName { get; set;}
    

    The other attributes you can use include: Range, RegularExpression and StringLength

  • Support for DefaultValueAttribute in Action-Method Parameters - This is yet another great new feature that allows you to pass default values as parameters to the action methods in your ASP.NET MVC 2.0 applications. Here is an example:

    public class EmployeeController 
    {
      public ActionResult View(int id, [DefaultValue(1)]int pageNumber) 
      {
    
      }
    }
    

Summary

In this article we examined the new and enhanced features in ASP.NET MVC 2.0 - a web application development framework based on the MVC Design Pattern. This article has provided an a head start to learning the new features and enhancements in ASP.NET MVC 2.0 for designing and implementing high performant, testable, maintainable, scalable, robust, web applications seamlessly. Happy reading!

References

http://www.asp.net/mvc
http://dotnetslackers.com/articles/aspnet/a-first-look-at-asp-net-mvc-2.aspx
http://msdn.microsoft.com/en-us/library/ee728598(v=VS.100).aspx

Related Articles





About the Author

Joydip Kanjilal

Microsoft Most Valuable Professional, Author and Speaker. Featured in "MSDN Featured Developer of the Fortnight (India)" a number of times. Winner of Community Credit Awards at www.community-credit.com several times. Authored numerous books and articles in Microsoft .NET and its related technologies. Authored the following books:-- ASP.NET 4.0 Programming (Mc-Graw Hill Publishing) Entity Framework Tutorial (Packt Publishing) Pro Sync Framework (APRESS) Sams Teach Yourself ASP.NET Ajax in 24 Hours (Sams Publishing) ASP.NET Data Presentation Controls Essentials (Packt Publishing)

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • It's not unusual for a company to use a variety of formal and informal file-sharing methods. Many methods are fraught with significant operational, financial, and legal risks in addition to other potentially negative business consequences. Strategic managed file transfer (MFT) software and services help connect the business "dots" between the various ad hoc and more systematic ways that files are moved. Read this white paper to learn how the right MFT solution, can bring order, structure, and, importantly, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds