Learn the ASP. MVC5 Framework Secret Notes-asp.net How MVC works (i)

Source: Internet
Author: User

ASP. NET MVCHow it works

aspbecause of the pipeline design, it has good expansibility and the wholeASP . NET MVCThe application framework is through the expansionASPimplemented. Through the aboveASPIntroduction to piping design we know thatASPthe extension points are mainly reflected inHttpModuleand theHttpHandlerOn top of these two core components, the entireASP . NET MVCthe framework is defined by a customHttpModuleand theHttpHandlerbuilt up.

Next we will simulate how ASP. NET MVC works by customizing the components.

1.4.1built in mini versionASP . NET MVCon theWebApplication

Before we introduce the implementation of our own ASP . NET MVC, let's take a look at how Web applications built on our own framework . We Build an empty asp . NET Web application with VS .

We first defined the followingSimplemoduletype, which represents the final need to bind to theViewon the data. In order to verify the targetControllerand theActionthe parsing mechanism,Simplemodulethe two attributes defined represent the target of the current request, respectivelyControllerand theAction. For a better demonstrationASP . NET MVCthe parameter binding mechanism (Modelbinding), we areSimplemoduledefines the additional3a PropertyFoo,Barand theBaz, 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 and action Span style= "font-family: Arial" > We define the following one inherited from the abstract class controllerbase homecontroller Span style= "font-family: Song Body" >. Defined in homecontroller action method index has a simplemodel actionresult last night return type.

public class Homecontroller:controllerbase    {public        actionresult Index (Simplemodel model)        {            Action <TextWriter> callback = writer +            +                writer. Write (String. Format ("Controller: {0}<br/>action: {1}<br/><br/>", model.) Controller, model. Action));                Writer. Write (String. Format ("Foo: {0}<br/>bar: {1}<br/>baz: {2}", model. Foo, model. Bar, model. Baz));            };            return new Rawcontentresult (callback);        }    }

Method Index Returns a Rawcontentresult object. the rawcontentresult object is the encapsulation of a action<textwriter> delegate that uses the latter to write the content that needs to be rendered. Here we will take the two sets of properties ofthe Simplemodel object as parameters (controller/action and foo/bar/ Baz) values are displayed.

ASP. NET MVCaccording to the requestedURLto parse the target.Controllerthe type andActionThe method name. Specifically, we will register some of the includedControllerand theActionname as a placeholder for the route template. If the request address conforms to the pattern of the corresponding address template, the targetControllerand theActionthe name can be parsed correctly. We areGlobal.asaxThe following template is registered in the{controller}/{action}"ofRouteobject. In addition, we have registered aControllerthe factory of the objectdefaultcontrollerfactory.

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 ());        }    }

Routing implements the requestURLand Targetcontroller/actionmapping between the. ASP . NET MVCthe route is based on theASPits own routing system, which is defined by a customHttpModuleto achieve. In ourASP . NET MVCFramework, we'll name itUrlRoutingModule, it withASPthe corresponding in the routing systemHttpModuletype with the same name, we configure the customHttpModuleto configure.

<system.webServer>     <modules>          <add name= "UrlRoutingModule" type= "Webapp.urlroutingmodule, WebApp "/>          </modules>      </system.webServer>

So the programming and configuration work is done.

It shows how to build a Web app on our own "mini-version" ASP. NETMVC Framework , where you can see it and create a real ASP. apply indistinguishable. We then step through the process of analyzing how this custom ASP. NET MVC Framework is built, and he represents the basic workings of the real ASP. NET MVC Framework.

OverallASP . NET MVCfollow this process to process the response request:ASP . NET MVCusing the routing system to requestURLto parse and get the target .Controllerand theActionand other appropriate routing data. It is based onControllerthe name resolves the targetControllerand activates it (by default, it is created based on the type's reflection mechanism .Controllerobject). Next,ASP . NET MVCUseActionname resolution is defined in the targetControllerthe corresponding method in the type, and then perform the activationControllerThis method of the object. Actionmethod can respond directly to the current request during execution, or it can return aActionResultobject to respond to the request. For the latter,ASP . NET MVCin accomplishing the goalActionafter the method executes, the returnedActionResultobject to make a final response to the current request.

In the next chapter we will explain the establishment of the routing system in this custom ASP.

Learn the ASP. MVC5 Framework Secret Notes-asp.net How MVC works (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.