In-depth understanding of ASP. net mvc (5)

Source: Internet
Author: User

Series directory

Review

The first four sections of the series deeply analyze the ASP. net url routing mechanism, and how MVC implements the areas Mechanism Based on it. At the same time, it involves many details about inbound and outbound. Mentioned in section 2ndMvcroutehandlerIs the entry to the MVC framework. This section beginsMvcroutehandlerLet's proceed.

 

Controller creation process: builder and factory

MvcroutehandlerOnly throughGethttphandlerMethod returnsMvchandlerInstance,MvchandlerObtain from routedataController nameCreatesControllerbuilderAnd through the controllerbuilderGetcontrollerfactoryReturnsIcontrollerfactoryThe instance isDefaultcontrollerfactory, ItsCreatecontrollerMethod is used to create the desired controller instance. The following sectionCodeFromMvchandler:

// Get the Controller typestring controllername = requestcontext. routedata. getrequiredstring ("controller"); // instantiate the controller and call executefactory = controllerbuilder. getcontrollerfactory (); Controller = factory. createcontroller (requestcontext, controllername );

DefaultcontrollerfactoryCreate a controller in two steps:

 
Type controllertype = getcontrollertype (requestcontext, controllername); icontroller controller = getcontrollerinstance (requestcontext, controllertype );

GetcontrollerinstanceFinally, the controller is created as follows:

Return (icontroller) activator. createinstance (controllertype );

This makes it very difficult to use di design patterns. Fortunately, we can override getcontrollerinstance and apply various di. Which of the following statements is used by an individual?Ninject.

In addition, to speed up the getcontrollertype method, defaultcontrollerfactory internally uses the cache mechanismProgramAnd all types of referenced assembly are cached as hash tables.

You can use the following method to optimize search by providing a higher priority for certain namespaces.

Protected void application_start () {registerroutes (routetable. routes); controllerbuilder. current. defaultnamespaces. add ("MyApp. controllers. * "); controllerbuilder. current. defaultnamespaces. add ("otherassembly. mynamespace. *");}

The priority of the namespace set above is still lower than the namespace specified for a route table. The areas mechanism is implemented by specifying the namespace when setting the route table. For more information, see ASP. net MVC (4 ).

You can also replace controllerfactory at the controllerbuilder level:

 
Protected void application_start () {registerroutes (routetable. routes); controllerbuilder. Current. setcontrollerfactory (New mycontrollerfactory ());}

 

Controller call: the controller is just a common. Net class.

MvchandlerAfter obtaining the Controller instance by using controllerbuilder and icontrollerfactoryExecuteMethod. Call the icontrollerfactoryReleasecontrollerClosing. From this we can see that the Controller's execute has done everything, and it seems to be "powerful". In fact, it is only a common class of. net. The MVC framework gives it "extraordinary" capabilities:

AllControllerYou only need to implement one interface,IcontrollerThe interface defines the execute method:

 
Namespace system. Web. MVC {using system. Web. Routing; public interface icontroller {void execute (requestcontext );}}

As can be seen from the interface definition, when the controller is "called", it should be responsible for completing the execute method, ParametersRequestcontextEncapsulatedHttpcontextSo you can directly implement a controller like the following and work as well:

Public class helloworldcontroller: icontroller {public void execute (requestcontext) {requestcontext. httpcontext. response. Write ("Hello, world! ");}}

However, the logic and architecture are much more complex than this one. Therefore, the MVC Framework provides the following class relationships:

Icontroller-> controllerbase-> Controller

Controllerbase implements execute. Execute internally calls executecore, and executecore serves as an abstract method, which is implemented in the controller. Controllerbase only provides methods such as tempdata and viewdata. The executecore method of controller actually invoke the action mechanism, which is the entry of action. The following code is the implementation of executecore:

Possiblyloadtempdata (); try {string actionname = routedata. getrequiredstring ("action"); If (! Actioninvoker. invokeaction (controllercontext, actionname) {handleunknownaction (actionname) ;}} finally {possiblysavetempdata ();}

We can see that routedata is here againProvides the action ParameterAs you can imagine, the invokeaction method calls action based on the action name and implements many verification mechanisms. Next, we will discuss the action logic.

Labor fruit, reproduced please indicate the source: http://www.cnblogs.com/P_Chou/archive/2010/11/20/details-asp-net-mvc-05.html

Related Article

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.