Export (0) Print
Expand All

MVC Framework and Application Structure

In an ASP.NET Web site, URLs typically map to files that are stored on disk (usually .aspx files). These .aspx files include markup and code that is processed in order to respond to the request.

The ASP.NET MVC framework maps URLs to server code differently than an ASP.NET Web Forms page. Instead of mapping URLs to ASP.NET pages or handlers, the framework maps URLs to controller classes. Controller classes handle incoming requests, such as user input and interactions, and execute appropriate application and data logic, based on user input. A controller class typically calls a separate view component that generates HTML output as the response.

The ASP.NET MVC framework separates the model, view, and controller components. The model represents the business/domain logic of the application, typically with data backed by a database. The view is selected by the controller and renders the appropriate UI. By default, the ASP.NET MVC framework uses the existing ASP.NET page (.aspx), master page (.master), and user control (.ascx) types for rendering to the browser. The controller locates the appropriate action method in the controller, gets values to use as the action method's arguments, and handles any errors that might occur when the action method runs. It then renders the requested view. By default, each set of components is in a separate folder of an MVC Web application project.

The ASP.NET MVC framework uses the ASP.NET routing engine, which provides flexibility for mapping URLs to controller classes. You can define routing rules that the ASP.NET MVC framework uses in order to evaluate incoming URLs and to select the appropriate controller. You can also have the routing engine automatically parse variables that are defined in the URL, and have the ASP.NET MVC framework pass the values to the controller as parameter arguments. For more information, see ASP.NET Routing.

ASP.NET MVC framework does not use the ASP.NET Web Forms postback model for interactions with the server. Instead, all end-user interactions are routed to a controller class. This maintains separation between UI logic and business logic and helps testability. As a result, ASP.NET view state and ASP.NET Web Forms page life-cycle events are not integrated with MVC-based views.

The ASP.NET MVC framework includes a Visual Studio project template that helps you create Web applications that are structured to support the MVC pattern. This template creates a new MVC Web application that is configured to have the required folders, item templates, and configuration-file entries.

NoteNote:

The ASP.NET MVC Web Application project templates are based on the ASP.NET Web Application project template. You select a new ASP.NET MVC project by selecting New Project from the File menu instead of by selecting New Web Site.

When you create a new MVC Web application, Visual Studio gives you the option to create two projects at the same time. The first project is a Web project where you implement your application. The second project is a unit-test project where you can write unit tests for the MVC components in the first project.

NoteNote:

Microsoft Visual Studio Standard Edition and Microsoft Visual Web Developer Express Edition do not support creating unit test projects. Therefore, they do not offer the option to create a test project when you create an MVC application.

You can use any unit-testing framework that is compatible with the .NET Framework in order to test ASP.NET MVC applications. Visual Studio Professional Edition includes testing-project support for MSTest. For more information about MSTest, see MSTest.exe Command-Line Options.

When you create an ASP.NET MVC Web application project, MVC components are separated based on the project folders shown in the following illustration:

mvc_FolderStructure

By default, MVC projects include the following folders:

  • App_Data, which is the physical store for data. This folder has the same role as it does in ASP.NET Web sites that use Web Forms pages.

  • Content, which is the recommended location to add content files such as cascading style sheet files, images, and so on. In general, the Content folder is for static files.

  • Controllers, which is the recommended location for controllers. The MVC framework requires the names of all controllers to end with "Controller", such as HomeController, LoginController, or ProductController.

  • Models, which is provided for classes that represent the application model for your MVC Web application. This folder usually includes code that defines objects and that defines the logic for interaction with the data store. Typically, the actual model objects will be in separate class libraries. However, when you create a new application, you might put classes here and then move them into separate class libraries at a later point in the development cycle.

  • Scripts, which is the recommended location for script files that support the application. By default, this folder contains ASP.NET AJAX foundation files and the jQuery library.

  • Views, which is the recommended location for views. Views use ViewPage (.aspx), ViewUserControl (.ascx), and ViewMasterPage (.master) files, in addition to any other files that are related to rendering views. The Views folder contains a folder for each controller; the folder is named with the controller-name prefix. For example, if you have a controller named HomeController, the Views folder contains a folder named Home. By default, when the ASP.NET MVC framework loads a view, it looks for a ViewPage (.aspx) file that has the requested view name in the Views\controllerName folder. By default, there is also a folder named Shared in the Views folder, which does not correspond to any controller. The Shared folder is used for views that are shared across multiple controllers. For example, you can put the Web application's master page in the Shared folder.

In addition to the folders listed previously, an MVC Web application uses code in the Global.asax file to set global URL routing defaults, and it uses the Web.config file to configure the application.

Routes are initialized in the Application_Start method of the Global.asax file. The following example shows a typical Global.asax file that includes default routing logic.


Public Class MvcApplication
    Inherits System.Web.HttpApplication

    Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}")

        ' MapRoute takes the following parameters, in order:
        ' (1) Route name
        ' (2) URL with parameters
        ' (3) Parameter defaults
        routes.MapRoute( _
            "Default", _
            "{controller}/{action}/{id}", _
            New With {.controller = "Home", .action = "Index", .id = ""} _
        )

    End Sub

    Sub Application_Start()
        RegisterRoutes(RouteTable.Routes)
    End Sub
End Class


Show:
© 2014 Microsoft