oXite源码学习导读二:Action的返回类型与IActionInvoker

我们先来看一下oXite里面一段Controller中Action方法的代码:

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

            return model;
        }

看出上面的Action方法有什么不妥么?怎么Action方法返回的类型为OxiteModelList<Area>的呢?Controller的Action方法返回的不是ActionResult或者为Void的么?难道Action的返回值还可以是任意类型的?相信你看到Controller里面Action方法这些代码的时候,你也会产生这样的疑问。

默认情况下Action的返回值应该是ActionResult(推荐)或者void。那么这里为什么可以返回一个数据集合呢?你或许会想OxiteModelList<Area>会不会是继承自ViewResult呢?很遗憾的OxiteModelList<Area>并没是继承自ViewResult。正当我郁闷的时候,忽然看到ContainerFactory里面对IActionInvoker进行了Ioc注册:

于是很兴奋的跑去OxiteControllerActionInvoker里面看个究竟,果然是在这里做了手脚:

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 new ViewResult { ViewData = controllerContext.Controller.ViewData, TempData = controllerContext.Controller.TempData }; }

OxiteControllerActionInvoker的继承关系如下
OxiteControllerActionInvoker =>> ControllerActionInvoker –>> IActionInvoker 。

OxiteControllerActionInvoker重写了ControllerActionInvoker的CreateActionResult方法,该方法有一个命名为actionReturnValue的参数,从参数的命名我们很容易就可以看出这个参数的意义,没错她就是我们Action中返回的结果,从上面的代码我们可以看到将actionReturnValue赋值给了ViewData.Model,然后重新构造一个ViewResult的结果返回。相信看完上面的代码你已经豁然开朗了。

然后在OxiteControllerFactoryController的ActionInvoker修改为OxiteControllerActionInvoker

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进行了注册:

 

Enjoy。By Q.Lee.lulu

转载于:https://www.cnblogs.com/QLeelulu/archive/2009/03/30/1425540.html

你可能感兴趣的:(oXite源码学习导读二:Action的返回类型与IActionInvoker)