MVC Framework and application structureintroduction
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
Typical ASP. NET web site. 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. (ASP. net mvc controllers implement a pattern known as the Front
Controller pattern.) A controller class typically calla separate View
Component that generates HTML output as the response.
The
ASP. net mvc Framework separates the model, view, and Controller
Components. The model component typically maintains state by persisting
Data in a database. The view component 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
Component 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
Requested view. By default, each set of components is in a separate
Folder of an MVC web application project.
URL Mapping
The
ASP. net mvc framework uses a URL-routing engine that provides
Flexibility for mapping URLs to Controller classes. You can define
Routing rules that the ASP. net mvc framework uses to evaluate incoming
URLs and to select the appropriate controller. You can also have
Routing engine automatically parse variables that are defined in
URL, and have the ASP. net mvc Framework pass the variable values to
Controller as parameter arguments. For more information, see ASP. NET routing and ASP. NET routing in MVC applications.
The MVC Framework and postbacks
ASP. NET
MVC Framework does not use the ASP. NET PostBack model for interactions
With the server. Instead, all end-user interactions are routed to
Controller class. This maintains separation between UI logic and
Business logic and helps testability. As a result, ASP. NET view State
And ASP. NET page life-cycle events are not integrated with MVC-based
Views.
Creating an ASP. net mvc application
The
ASP. net mvc framework between des A Visual Studio Project template that
Helps you create Web applications that are structured to support
MVC pattern. This template creates a new MVC web application that is
Configured to have the required folders, item templates, and
Configuration-file entries.
Note
The
ASP. net mvc web application templates are based on the ASP. NET web
Application 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
Option to create two projects at the same time. The first project is
Web project where you can implement your application. The second
Project is a testing project where you can write unit tests for your
MVC components.
Note
Visual
Studio 2008 Standard Edition does not offer the option to create a test
Project when you create an MVC application project.
You
Can use any unit-testing framework that is compatible with the. NET
Framework in order to test ASP. net mvc applications. Visual Studio 2008
Professional login des testing-project support for mstest. For more
Information about mstest, see mstest.exe command-line options on the msdn web site.
Web application MVC project structure
When
You create an ASP. net mvc web application project, MVC components are
Separated based on the project folders shown in the following figure:
The folders used in MVC applications are as follows:
App_data.
The app_data folder is the physical store for data. This folder has
Same role as it does in ASP. NET web sites that use web forms pages.
Content
Folder. The content folder is the recommended location to add content
Files such as scripts, Cascading Style Sheet files, images, and so on.
In general, the content folder is for static files.
Controllers
Folder. The controllers folder is the recommended location
Controllers. The MVC Framework requires the names of all controllers
End with "controller"-for example, homecontroller, logincontroller, or productcontroller.
Models
Folder. The models folder is provided for classes that represent
Application Model for your MVC web application. This usually provided des
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.
Views
Folder. The views folder is the recommended location for views. Views
Use. aspx,. ascx, And. 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 will contain a folder named Home. By default, when
ASP. net mvc Framework loads a view, it looks for an. 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 using SS multiple controllers. For example, you can put
Web application's master page in the shared folder.
In
Addition to the folders listed previusly, an MVC web application uses
The global. asax file to set global URL routing defaults, and it uses
The Web. config file to configure the application.
Setting global URL routing defaults
Routes are initialized in the application_start
Method of the global. asax. CS or global. asax. VB file. The following
Example shows a typical global. asax file that includes des default Routing
Logic.
Public Class GlobalApplication
Inherits System.Web.HttpApplication
Shared Sub RegisterRoutes(routes As RouteCollection)
' Note: Change the URL to "{controller}.mvc/{action}/{id}" to
' enable automatic support for IIS6 and for IIS7 classic mode.
' Handler to stop URL routing for Web resources.
routes.Add( _
New Route( _
"{resource}.axd/{*pathInfo}", _
New StopRoutingHandler()) _
)
' MapRoute takes the following parameters, in order:
' (1) Route name
' (2) URL with parameters
' (3) Parameter defaults
' (4) Parameter constraints
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
public class GlobalApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
// Note: Change the URL to "{controller}.mvc/{action}/{id}" to
// enable automatic support for IIS6 and for IIS7 classic mode.
// Handler to stop URL routing for Web resources.
routes.Add(
new Route(
"{resource}.axd/{*pathInfo}",
new StopRoutingHandler())
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}