Learning the secrets of ASP. NET MVC5 framework-How ASP. net mvc runs (1)
How ASP. net mvc runs
ASP. NET adopts a pipeline design, so it has good scalability. The entire ASP. net mvc application framework is implemented by extending ASP. NET. ASP. NET pipeline design we know, ASP. NET extension points are mainly reflected in the HttpModule and HttpHandler core components, the entire ASP.. net mvc framework is built through the custom HttpModule and HttpHandler.
Next, we use custom components to simulate the operating principles of ASP. net mvc.
1.4.1 Web applications built on ASP. net mvc mini edition
Before introducing the implementation principles of ASP. net mvc, let's take a look at how to build Web applications built on our own framework. We use VS to create an empty ASP. NET Web application.
First, we define the next SimpleModule type, which indicates the data to be bound to the View. To verify the resolution mechanism of the Target Controller and Action, the two attributes defined by SimpleModule indicate the target Controller and Action of the current request respectively. To better demonstrate ASP. net mvc parameter binding mechanism (Model binding), we define three additional attributes Foo, Bar, and Baz for SimpleModule, and let them have different data types.
public class SimpleModel { public string Controller { get; set; } public string Action { get; set; } public string Foo { get; set; } public int Bar { get; set; } public double Baz { get; set; } }
Next we define the Controller and Action. We define the HomeController that inherits from the abstract class ControllerBase, for example. Define the Action method Index in HomeController to have a SimpleModel type input parameter, and return the type with ActionResult last night.
public class HomeController : ControllerBase { public ActionResult Index(SimpleModel model) { Action
callback = writer => { writer.Write(string.Format("Controller: {0}Action: {1}", model.Controller, model.Action)); writer.Write(string.Format("Foo: {0}Bar: {1}Baz: {2}", model.Foo, model.Bar, model.Baz)); }; return new RawContentResult(callback); } }
Method Index Returns a RawContentResult object. RawContentResult object is an Action The encapsulation of the Delegate, which uses the latter to write the content to be rendered. Here we will display the values of the two sets of attributes (Controller/Action AND Foo/Bar/Baz) of the SimpleModel object as the parameter.
ASP. net mvc parses the type and Action method name of the target Controller based on the request URL. Specifically, we will register some routing templates containing the Controller and Action names as placeholders. If the request address conforms to the corresponding address template mode, the name of the target Controller and Action can be correctly parsed. We have registered a Route object in the Global. asax template, for example, {Controller}/{Action. In addition, we also registered a factory DefaultControllerFactory for creating the Controller object.
public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { RouteTable.Routes.Add("default", new Route { Url = "{controller}/{action}" }); ControllerBuilder.Current.SetControllerFactory(new DefaultControllerFactory()); } }
The routing implements the ing between the request URL and the Target Controller/Action. ASP. net mvc routes are built on ASP. NET's own routing system, and the latter is implemented through the custom HttpModule. In our ASP. in the. net mvc Framework, we name it UrlRoutingModule, which corresponds to ASP. the corresponding HttpModule type in the. NET routing system has the same name. We configure the custom HttpModule through configuration.
This completes programming and configuration.
The above demonstrates how to create a "mini version" ASP. net mvc framework to build a Web application, from which you can see it and create a real ASP. net mvc applications. Next, we will analyze how the custom ASP. net mvc framework is built. It also represents the basic working principle of the real ASP. net mvc framework.
In general, ASP. net mvc processes the Response Request according to this process: ASP. net mvc parses the request URL using the routing system to obtain the name of the target Controller and Action, and other corresponding routing data. It resolves the real type of the Target Controller Based on the Controller name and activates it (by default, it creates a Controller object based on the Type reflection mechanism ). Next, ASP. net mvc uses the Action name to parse the corresponding method defined in the target Controller type, and then executes this method to activate the Controller object. The Action method can directly respond to the current request during execution, or return an ActionResult object to respond to the request. For the latter, ASP. net mvc will execute the returned ActionResult object to make the final response to the current request after the target Action method is executed.
In the next chapter, we will explain how to establish a Routing System in the Custom ASP. net mvc.