Objective
In the last article, we implemented the first ASP.net MVC page. For those who have not contacted this framework, may be confused in some places, so this article I will be illustrated with the text of the method, standing in the overall perspective of some asp.net MVC operating mechanism, so as to help friends better understand the follow-up article. ^_^
Global
First we look at a picture, because this picture is my own painting, not from Microsoft official, so if there is anything not in place also hope Haihan!
First, the user sends a URL request to the server through a Web browser, where the requested URL is no longer a xxx.aspx format, but rather a http://HostName/ControllerName/ActionName/Parameters. This request was intercepted by the ASP.net MVC routing mapping system. (Routing mappings can be configured in Global.asax, we'll talk later) the Routing Mapping system resolves the controller name Controllername,action name actionname and each parameter parameters according to the mapping rules, and then, Looking for the ControllerNameController.cs controller class in the Controllers directory, by default, the system always looks for the "controller name +controller" in the Controllers directory, and then, Find a method under this class that has the same name as ActionName, and when found, pass the parameters as a parameter to this method, and then the action method begins execution, returns to the corresponding view when it is completed, and by default returns the views directory with the Controllername an aspx file with the same name as ActionName under a directory with the same name, and passes ViewData to the view. The viewdata typically contains control of the control view display and the data needed for the view display.
We follow the above ideas to review the previous page of the request process. The URL we are passing is http://localhost/Home/Index. Under the default routing rule, set Controllername to "Home", ActionName set to "Index", and no parameters. So the system to find the controllers directory of the HomeController class index method, successfully found, and then executed. This method invokes the mock model to fetch some data and put it into the ViewData corresponding key value entry. Then return to the view, which is the index.aspx under the views of the home. This view takes out the data in ViewData and renders it in a certain format, thus completing a typical asp.net mvc call.
Routing
As you can see from the above, routing in ASP.net mvc is important. It directly determines how URLs are parsed, and therefore determines how the system works. So here we are to uncover the veil of mystery of routing.
Open our demo under the Global.asax.cs file, you can see the following code:
Global.asax.cs:
1using System;
2using System.Collections.Generic
3using system.linq;
4using system.web;
5using SYSTEM.WEB.MVC;
6using System.Web.Routing;
7
8namespace mvcdemo
9{
Ten//note:for instructions on enabling IIS6 or IIS7 Classic mode,
One//V Isit http://go.microsoft.com/? linkid=9394801
public class MvcApplication:System.Web.HttpApplication
{
public static V OID RegisterRoutes (routecollection routes)
{
routes. Ignoreroute ("{resource}.axd/{*pathinfo}");
Routes
. Maproute (
"Default",//Route name
"{Controller}/{action}/{id}", URL with parameters
New {controller = "Home", action = "Index", id = ""}//Parameter defaults< br> 23);
protected void Application_Start ()
{
RegisterRoutes (routetable. Routes);
30}
31}
}