Resources

MVC Applications in ASP.NET websites

Can combine ASP.NET and MVC in same site.

Model using postback and ViewState non-existent in MVC.

MVC uses routing engine and set of controllers to control processing.

MVC does retain master pages, style sheets, membership and standard page markup.

Architecture

Request to MVC handled by the UrlRotingModule which parses requests and selects a route for request based on previously defined configuration.

Request is routed to one of the controller classes that you write to manage processing.

The controller will access your data (the model), connect it for display (the view) and send response back to user.

By writing the model, view and controllers have finder control over how each request is handled when compared to ASP.NET.

Request Life Cycle

Much of the request processing operations found in System.Web.Routing namespace

UrlRoutingModule acts as font controller for site. It takes request, looks up Route object from RouteTable. RouteData and RequestContext objects then created to represent the route and request.

Processing of the RequestContext then passed to classes in System.Web.Mvc namespace.

The MvcRouteHandler handles routing to your MVC controller class by creating an instance and calling its Execute method.

Routing Basics

The URI http://muApp/Employees/Detail/5 sees the portion of request beyond myApp being processed by routing engine.

By default this URI would be processed as controller/location/id - Employees maps to EmployeesController, the action would map to Detail in the URI which equates to the Detail method on the EmployeesController. The value 5 would be passed as a parameter to the Detail method.

Controller class may look like:

public class EmployeeController : Conreoller
{
public ActionResult Details (int id)
{
return View();
}
}

Controller can define as many action methods as required.

Correspond to individual user actions, e.g. saving data or navigating to another page.

Most action return an instance of class inheriting from System.Web.Mvc.ActionResult, most commonly ViewResult which simply returns a webpage.

The base Controller class contains helper methods for returning results, e.g. to return a ViewResult call the View() method.

Application structure

Different structure to traditional ASP.NET website.

Create via Add New Project dialogue.

Creates associated Unit Test Project so that controller tests can be defined - basic tests for Account controller and home page will be automatically generated.

Models, views and controllers grouped into series of folders.

Application Areas

For very large sites can be difficult to keep all models, views and controllers in single set of folders.

Can define different MVC project areas within application.

Area is sub-set of project based on logical grouping.

Create by right-clicking on project and choosing Add | Area.

Add models, views and controllers to these individual areas.

Each area has separate namespace.

Area includes AreaRegistration file that maps files in area into routing engine.

Simple MVC Webpage

Many apps do not require a model - controller may just handle navigation and simply display view.

Initial project generates HomeController and Home folder in Views folder.

HomeController has two actions - Index and About which return a ViewResult instance.

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

public ActionResult About()
{
return View();
}
}

Routing engine maps requests for http://MySite/home/index to the controller. Note, special route added that also maps http://MySite to same controller.

The return View() statement tell MVC to look in folder with same name as controller (Home) for view with same name as action method (Index.aspx) and return it as a response.

The Views/Home/Index.aspx is built from a master page:

<asp:Content ID=2Content2" ContentPlaceHolderID=2MainContent" runat="server>

<h2><%: ViewData["message"] %></h2>

</asp:Content>

Downloads