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.
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.
- Model - data and related business logic / validation rules. Could be LINQ to SQL, Entity Framework or custom layer that provides access to data and business rules
- View - indicates how data from model will be displayed. Set of webpages and user controls that define layout. Do not include input logic or processing of user actions - this is handled by controller. This separation makes input logic processing easier to unit test.
- Controller - handles user interaction (input logic) and the connection between model and view.
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.
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)
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.
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.
- Contents - used for static data files, e.g. images. Can also house CSS and standard HTML files
- Controllers - houses controller classes. Each entity in model has a single controller. May also define additional controllers for other types of processing, e.g. LoginController, HomeController, NavigationController. Follow a naming convention of _Entity_Controller.
- Models - code representing business model. Code interacts with databases and processes business rules. If model is Entity Framework or LINQ to SQL then place DBML or EDMX file here. Model could be in separate DLL - in which case the folder should be left empty
- Views - contains application views - .aspx pages, .ascx controls and master pages. Create sub-folder for each controller in application, e.g. Employee for EmployeeController. Sub-folders contain views rendering different user activity related to that controller. Should also contain Shared sub-folder for views shared across controllers
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.
public class HomeController : Controller
public ActionResult Index()
ViewData["message"] = "Welcome to ASP.NET MVC";
public ActionResult About()
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>