Oxite source code learning Introduction 2: return type of action and iactioninvoker

Source: Internet
Author: User

Let's take a look at the Action Method in the Controller in oxite.Code:

 Namespace Oxite. controllers { Public class  Areacontroller : Controller {[ Actionname ( "Find" ), Acceptverbs ( Httpverbs . Post)] Public Virtual  Oxitemodellist < Area > Findquery (Areasearchcriteria Searchcriteria ){ Ilist < Area > Foundareas = areaservice. findareas (searchcriteria ); Oxitemodellist < Area > Model = New  Oxitemodellist < Area > {List = foundareas}; model. addmodelitem (searchcriteria ); ReturnModel; }

Is there anything wrong with the above action method? How does the action method return a typeOxitemodellist<Area>? Isn't actionresult or void returned by the Controller's action method? Can the return value of action be of any type? I believe that when you see the action method code in the controller, you will also have such questions.

By default, the return value of action should be actionresult (recommended) or void. So why can we return a data set? You may thinkOxitemodellist<Area> Will it be inherited fromViewresultWhat about it? Sorry.Oxitemodellist<Area> Not inherited fromViewresult. When I was depressed, I suddenly sawContainerfactoryIactioninvoker is registered in IOC:

So I was very excited.OxitecontrolleractioninvokerLet's take a look at the details here:

 Namespace Oxite. Infrastructure {Public class  Oxitecontrolleractioninvoker : Controlleractioninvoker { Private readonly  Ifilterregistry Filterregistry; Public Oxitecontrolleractioninvoker ( Ifilterregistry Filterregistry ){ This . Filterregistry = filterregistry ;} Protected override  Actionresult  Createactionresult 
( Controllercontext Controllercontext, Actiondescriptor Actiondescriptor, Object Actionreturnvalue ){ If (Actionreturnvalue = Null ) {Controllercontext. Controller. viewdata. Model = New Oxitemodel {Container = New Notfoundpagecontainer ()}; Return new Notfoundresult ();} If ( Typeof ( Actionresult ). Isassignablefrom (actionreturnvalue. GetType ())) Return Actionreturnvalue As Actionresult ; Controllercontext. Controller. viewdata. Model = actionreturnvalue;Return newViewresult{Viewdata = controllercontext. Controller. viewdata, tempdata = controllercontext. Controller. tempdata }; }

OxitecontrolleractioninvokerThe inheritance relationships of are as follows::
Oxitecontrolleractioninvoker => Controlleractioninvoker-> iactioninvoker.

OxitecontrolleractioninvokerRewrittenControlleractioninvokerThe createactionresult method of. This method has a parameter named actionreturnvalue. From the parameter name, we can easily see the meaning of this parameter. Yes, she is the result returned by our action, from the code above, we can see that actionreturnvalue is assigned to viewdata. model, and then re-constructViewresult. I believe that after reading the above Code, you have suddenly become enlightened.

ThenOxitecontrollerfactoryPairControllerActioninvokerOxitecontrolleractioninvoker:

 Namespace Oxite. Infrastructure {Public class  Oxitecontrollerfactory : Defaultcontrollerfactory { Private readonly  Iunitycontainer Container; Public Oxitecontrollerfactory ( Iunitycontainer Container ){ This . Container = container ;} Protected override  Icontroller Getcontrollerinstance ( Type Controllertype ){Icontroller Icontroller = container. Resolve (controllertype) As  Icontroller ; If ( Typeof ( Controller ). Isassignablefrom (controllertype )){ Controller Controller = icontroller As  Controller ; If (Controller! = Null )Controller. actioninvoker = container. Resolve <Iactioninvoker> ();                  Return Icontroller ;} Return Icontroller ;}}}

Iactioninvoker has been registered previously:

 

Enjoy. By Q. Lee. Lulu

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.