DefaultControllerFactory的GetControllerInstance方法中的RequestContext参数用于什么? [英] What the RequestContext parameter in GetControllerInstance method of DefaultControllerFactory is for?

查看:140
本文介绍了DefaultControllerFactory的GetControllerInstance方法中的RequestContext参数用于什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了这种方法

protected internal virtual IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        if (controllerType == null)
        {
            throw new HttpException(404,
                                    String.Format(
                                        CultureInfo.CurrentCulture,
                                        MvcResources.DefaultControllerFactory_NoControllerFound,
                                        requestContext.HttpContext.Request.Path));
        }
        if (!typeof(IController).IsAssignableFrom(controllerType))
        {
            throw new ArgumentException(
                String.Format(
                    CultureInfo.CurrentCulture,
                    MvcResources.DefaultControllerFactory_TypeDoesNotSubclassControllerBase,
                    controllerType),
                "controllerType");
        }
        return ControllerActivator.Create(requestContext, controllerType);
    }

,我想知道requestContext参数用于什么,因为它似乎从未使用过.它已传递给ControllerActivator.Create,但也从未真正使用过:

and I'm wondering what the requestContext parameter is for since it seems it's never used. It's passed to ControllerActivator.Create but it never really used there either:

public IController Create(RequestContext requestContext, Type controllerType)
        {
            try
            {
                return (IController)(_resolverThunk().GetService(controllerType) ?? Activator.CreateInstance(controllerType));
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(
                    String.Format(
                        CultureInfo.CurrentCulture,
                        MvcResources.DefaultControllerFactory_ErrorCreatingController,
                        controllerType),
                    ex);
            }
        }

推荐答案

ControllerFactory的目的是根据需要创建Controller实例.为了使其能够确定适当的Controller类型,它可能需要RequestContext数据.

The purpose of the ControllerFactory is to create Controller instances on demand. In order for it to be able to determine the appropriate Controller type it may need the RequestContext data.

现在,DefaultControllerFactory(这是默认的Factory)确实确实在使用RequestContext引发了Http 404 Not Found异常(需要时),但是其他控制器工厂(必须实现IControllerFactory)可能使用它.

Now, the DefaultControllerFactory (which is the default Factory) is indeed using the RequestContext only to throw the Http 404 Not Found exception (when needed), but other Controller Factories (that have to implement IControllerFactory) may use it.

例如,UnityControllerFactory():

public class UnityControllerFactory : DefaultControllerFactory
    {
        public override IController CreateController(RequestContext context, string controllerName)
        {
            Type type = GetControllerType(context, controllerName);

            if (type == null)
            {
                throw new InvalidOperationException(string.Format("Could not find a controller with the name {0}", controllerName));
            }

            IUnityContainer container = GetContainer(context);
            return (IController)container.Resolve(type);
        }

        protected virtual IUnityContainer GetContainer(RequestContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            var unityContainerAccessor = context.HttpContext.ApplicationInstance as IUnityContainerAccessor;
            if (unityContainerAccessor == null)
            {
                throw new InvalidOperationException(
                    "You must extend the HttpApplication in your web project and implement the IContainerAccessor to properly expose your container instance");
            }

            IUnityContainer container = unityContainerAccessor.Container;
            if (container == null)
            {
                throw new InvalidOperationException("The container seems to be unavailable in your HttpApplication subclass");
            }

            return container;
        }

这篇关于DefaultControllerFactory的GetControllerInstance方法中的RequestContext参数用于什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆