ASP.NET MVC 2 Overview

by Greg Brewer

Background

The ASP.NET MVC Framework is the Microsoft implementation of the Model-View-Controller pattern for the .Net platform. The MVC pattern has been popularized by the Apache Struts, Ruby on Rails and many other frameworks. ASP.NET MVC Framework is designed to address one of the problems with the ASP.NET Web form model—the separation between the application UI and business logic allowing for independent design, implementation and testing of each component. The Model represents the state of application and contains the business logic; the Controller is responsible for updating the Model and passing information to the View; the View is responsible for rendering the user interface representing the Model.

Architecture

Controller

In the ASP.NET MVC Framework, Controllers are responsible for handling the request made to the application. In the ASP.NET MVC Framework there is nothing that directly corresponds to a page as you would think about in an ASP.NET Web form application. Instead of mapping application requests to a file on disk representing the page, requests are mapped to Controller Actions. The Controller is a class that exposes these Actions.

For example, the request for http://localhost/Store/Details/5 would be mapped to the Details Action of the Store Controller with a parameter of 5. Controller Actions are public methods of the Controller class. Any public method you add to a Controller is automatically added as a Controller action so be careful since a Controller method can be executed by just typing in the right URL.

A Controller returns an instance of a class that derives from ActionResult. The framework provides many built-in types that derive from the ActionResult class. One of the more common ActionResult classes is the ViewResult which is used to display a specific View as a result of the requested action. Some other action results redirect to another controller, return a JSON object or return a file stream. Typically you do not return an ActionResult directly; instead you use a helper method to construct the ActionResult. One example is calling the View method to create a ViewResult object.

Example 1

public class HomeController : Controller
{
  public ActionResult Index()
  {
    ViewData["Message"] = "Welcome to ASP.NET MVC!";
    return View();
  }
}

In the ASP.NET MVC Framework, Views are the closest things to a page. Views are responsible for displaying the current state of the application to a user.

Views are associated with controllers and actions based on specific folder and file names. For example, invoking the View() method with no parameters from the http://localhost/Home/Index controller returns the View located at ViewsHomeIndex.aspx. You can also specify a View name explicitly: View("Test"); which would return the View at ViewsHomeTest.aspx.

A View is just a standard (X)HTML document that can contain scripts and use Master Pages. In ASP.NET 4 the <%: %> syntax should be used instead of the <%= %> since it automatically HTML Encodes the results.

Example 2

<%@ Page Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1"
ContentPlaceHolderID="TitleContent" runat="server">
  Home Page
</asp:Content>
<asp:Content ID="Content2"
ContentPlaceHolderID="MainContent" runat="server">
  <h2><%: ViewData["Message"] %></h2>
</asp:Content>

Microsoft created HTML Helper methods to make it easier to add content to a View. The HTML Helper functions are called from the HTML property of the View. They can greatly reduce the amount of HTML markup and script that you need to produce.

Example 3

 <% using (Html.BeginForm()) { %>
<div>
  <fieldset>
    <legend>Account Information</legend>
    <div class="editor-label">
      <%: Html.LabelFor(m => m.UserName) %>
    </div>
    <div class="editor-field">
      <%: Html.TextBoxFor(m => m.UserName) %>
      <%: Html.ValidationMessageFor(m => m.UserName) %>
    </div>
...

The controller ViewData property is used to pass information from the controller to the View. The VIewData property is a collection of name/value pairs. Example 1 (above) shows the string value "Welcome to ASP.NET MVC!" being added with the name "Message." ViewData is passed to the View automatically. Example 2 (above) is displaying the value that was assigned.

Model

In contrast to the Controller and Views, the Model is not a defined set of classes or interfaces. The Model can be implemented using whatever means best suit the application. The ASP.NET MVC Framework places no restrictions on the objects that make up the Model for the application. If the Model is part of the main application, it is recommended to put the Model classes under the Model folder in the project. In larger systems the Model objects are often contained in separate assemblies so they can be reused in other projects.

The Controller typically interfaces with the Model by instantiating the Model classes in the Controller Action and calling methods to manipulate the Model and then passing information from the Model to a View for display.

What's New in ASP.NET MVC 2

The ASP.NET MVC 2 Framework is included in Visual Studio 2010. It is also available as a download for Visual Studio 2008, and an upgrade wizard is provided to update ASP.NET MVC 1 applications to ASP.NET MVC 2. The new features in MVC 2 are designed to increase developer productivity and include:

  • New Strongly Typed HTML Helpers
  • Enhanced Model Validation support across both server and client
  • Auto-Scaffold UI Helpers with Template Customization
  • Support for splitting up large applications into "Areas"
  • Asynchronous Controllers support that enables long running tasks in parallel
  • Support for rendering sub-sections of a page/site using Html.RenderAction

See What’s New in ASP.NET MVC 2 for more detailed information about these and other new features.

Tutorial

The MVC Music Store application is a tutorial built on the ASP.NET MVC 2 framework. It is a sample application that highlights some of MVC 2 productivity features as well as utilizing the Entity Framework 4 for its data access layer. It includes an 80 page Walkthrough tutorial illustrating the process of building the application.

info@stoutsystems.com
877.663.0877
© Copyright 1995-2022 - STOUT SYSTEMS DEVELOPMENT INC. - All Rights Reserved
envelopephone-handsetlaptop linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram