Top 7 Features of the ASP.NET MVC Framework

Introduction

ASP.NET MVC framework has been a real advantage for developers to build ASP.NET applications implementing MVC pattern easy, fast and in an efficient manner. Going forward in a project development the manageability of the project becomes much easier because of its loosely coupled nature. In this article I will take you through a list of ASP.NET MVC framework features which I feel to be the top 7 list.

ASP.NET MVC was initially introduced by Microsoft along with .NET framework 3.5 service pack 1 (1.0 version). Now with .NET Framework 4.0 and Microsoft Visual Studio 2010 it comes by default with some added features like templating, areas, etc as ASP.NET MVC2 (2.0 version).

The whole concept revolves around 3 major components:

  1. Model (Represents M)
    Model is generally the business objects responsible for representing the underlying the database schema and would hold the data/state for an MVC application.
  2. View (Represents V)
    This is the UI piece of the application. Generally in ASP.NET MVC application it is a .aspx and .ascx file.
  3. Controller (Represents C)
    This is the heart of ASP.NET MVC framework application which is responsible for rendering the appropriate view to the client, executing the pertinent action method, getting the data from the model and populating the view, getting the data from view and updating the model, etc.

Fig 1.0 is the diagrammatic representation of MVC pattern.

ASP.NET MVC diagrammatic representation of MVC pattern
Fig 1.0

Opens the Gate for Parallel Development

When you create either ASP.NET MVC application or Empty ASP.NET MVC application in Microsoft Visual Studio 2010 you will notice that the Visual Studio IDE creates the folders named Controllers, Views and Models by default and adds it to the solution. The model, controller and view have to be physically separated into different files. Fig 2.0 is the sample solution explorer screenshot.

ASP.NET MVC sample solution explore
Fig 2.0

In major organizations a project constitutes a separate UI team for designing the UI, a Dev team to write the core .NET code and also a data team (managing the model). The main advantage of this loosely coupled architecture is that it allows the different teams of a project to work on its own area without any dependency from another team. For example the UI team can solely work on only the Views without screwing up the code behind or waiting for some C# developer to check-in his changes.

Thus the ASP.NET MVC framework decreases the complexities involved in doing parallel development.

URL Routing - Controllers Serving the Request

Unlike a normal ASP.NET application, the ASP.NET MVC application requests are not made to the files on disk like the .aspx file. Rather the request is made directly to the controller. Below is a sample request URL format.

  http://localhost:34299/<Controller>/<ActionMethodName>

The incoming request would fire the ActionMethod of the controller that is specified in the URL, i.e. the request will be routed to the respective controller's action method and that method takes the responsibility of rendering back the result view. Why does this matter for a developer? Below is the answer.

  • Similar to REST-like URL based architecture. It also leads to easy manipulation of the URL by the users.
  • Makes the application search engine friendly which is considered to be more important in modern day web application development.

What makes this kind of URL driven architecture possible? The secret lies in the global.asax file. Check out the code below.

  public class MvcApplication : System.Web.HttpApplication
  {
          public static void RegisterRoutes(RouteCollection routes)
          {
              routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  
              routes.MapRoute(
                  "Default", // Route name
                  "{controller}/{action}/{id}", // URL with parameters
                  new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
              );
  
          }
  
          protected void Application_Start()
          {
              AreaRegistration.RegisterAllAreas();
  
              RegisterRoutes(RouteTable.Routes);
          }
  }

In the above code you might have noticed how the URL routing is registered and also the default values specified.



Top 7 Features of the ASP.NET MVC Framework

Support for Test Driven Development

As the MVC architecture is loosely coupled the massive advantage which you get out of it is building the unit test cases becomes straight forward. It makes test driven development very possible. For instance if you take the normal .aspx.cs page and you want to build unit test cases for the same then the complexity factor is high because of the following.

  • You have to create the instance for your page code behind class.
  • The code behind would be referring to the ASP.NET controls.
  • Difficult to create unit test cases for events like GridView_RowDataBound.

In MVC pattern there is no code behind the file or UI elements referred in the controller which makes the task much easier. All you have to do is point to the ActionMethod of the controller, right click and select Create Unit Tests as shown in Fig 3.0.

[click3.jpg]
Fig 3.0

Availability of HTMLHelper Methods

ASP.NET MVC views are meant to be HTML driven and you don't have to use any ASP.NET server controls. The ASP.NET MVC thus exclusively provides a bunch of HTMLHelper methods which makes the view development task easy. The best part is that the developers are provided with 100% intellisence for these HTML helper methods. Fig 4.0 shows a sample screenshot.

[account4.jpg]
Fig 4.0

In the above screenshot you might notice that the lambda expression kind of thing in the checkbox, textbox, etc. creation helper methods are to specify that the controls are created for that particular field of the injected model.

ASP.NET MVC CRUD Implementation: Scaffold Available

In day to day application development, developers spend most of their time in writing the code for the same Create, Read, Update and Delete operations on data entities. ASP.NET MVC framework provides a scaffold for performing the CRUD operations. While you create a controller there is a checkbox provided with which the developer can check for the framework to create the action methods for performing the CRUD operations. Fig 5.0 shows the respective screenshot.

[add5.jpg]
Fig 5.0

Below is the sample generated code.

  public class MyController : Controller
  {
          //
          // GET: /My/
  
          public ActionResult Index()
          {
              return View();
          }
  
          //
          // GET: /My/Details/5
  
          public ActionResult Details(int id)
          {
              return View();
          }
  
          //
          // GET: /My/Create
  
          public ActionResult Create()
          {
              return View();
          } 
  
          //
          // POST: /My/Create
  
          [HttpPost]
          public ActionResult Create(FormCollection collection)
          {
              try
              {
                  // TODO: Add insert logic here
  
                  return RedirectToAction("Index");
              }
              catch
              {
                  return View();
              }
          }
          
          //
          // GET: /My/Edit/5
   
          public ActionResult Edit(int id)
          {
              return View();
          }
  
          //
          // POST: /My/Edit/5
  
          [HttpPost]
          public ActionResult Edit(int id, FormCollection collection)
          {
              try
              {
                  // TODO: Add update logic here
   
                  return RedirectToAction("Index");
              }
              catch
              {
                  return View();
              }
          }
  
          //
          // GET: /My/Delete/5
   
          public ActionResult Delete(int id)
          {
              return View();
          }
  
          //
          // POST: /My/Delete/5
  
          [HttpPost]
          public ActionResult Delete(int id, FormCollection collection)
          {
              try
              {
                  // TODO: Add delete logic here
   
                  return RedirectToAction("Index");
              }
              catch
              {
                  return View();
              }
          }
  }

Templating Feature

The templating feature was introduced in ASP.NET MVC framework 2.0 version. This feature allows you to create a shared partial view and use the same on different other views. The HTML helper methods used for the edit templating feature are Html.EditorFor, Html.Editor and Html.EditorForModel.

Data Annotation Attributes For Validation

ASP.NET MVC framework provides a few data annotation attributes which are used for data validation purpose. These attributes are designed to decorate the fields exposed by the models. Below are some of them.

  1. Required
  2. DisplayName
  3. StringLength
  4. ScaffoldColumn
  5. Range, etc

Conclusion

I hope this article has showcased the top 7 features of ASP.NET MVC framework. Templating and data validation features of ASP.NET MVC framework are a little vast which I will be covering in my future articles. Readers could always make use of the comments section for providing their valuable input. If you need any specific areas to be covered in ASP.NET MVC please drop a note so that I could try addressing it.

Happy reading! See you in my next article.

Related Articles





About the Author

V.N.S Arun

I work for an MNC in Bangalore, India. I am fond of writing articles, posting answers in forums and submitting tips in dotnet. To contact me please feel free to make use of the "Send Email" option next to the display name.

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

  • As mobile devices have pushed their way into the enterprise, they have brought cloud apps along with them. This app explosion means account passwords are multiplying, which exposes corporate data and leads to help desk calls from frustrated users. This paper will discover how IT can improve user productivity, gain visibility and control over SaaS and mobile apps, and stop password sprawl. Download this white paper to learn: How you can leverage your existing AD to manage app access. Key capabilities to …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds