使用默认的控制器容器,如果统一注册的类型还没有得到确定这个电流控制器 [英] Use default controller container if Unity registered types has not got this current controller defined

查看:461
本文介绍了使用默认的控制器容器,如果统一注册的类型还没有得到确定这个电流控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次使用Unity,

我已经建立了这样一个类,并在Global.asax中注册的:

 公共类UnityControllerFactory:DefaultControllerFactory
{
    私人IUnityContainer容器;    公共UnityControllerFactory()
    {
        集装箱=新UnityContainer();
        RegisterTypes();
    }    保护覆盖一个IController GetControllerInstance(
      System.Web.Routing.RequestContext的RequestContext,
      键入controllerType)
    {
        返回controllerType == NULL?
            空值 :
            (一个IController)container.Resolve(controllerType);
    }    私人无效RegisterTypes()
    {
        container.RegisterType< IUserRepository,EFUserRepository>();
    }
}

问题是,当的AccountController(默认MVC项目自带)被调用,它抛出一个错误:


  

类型'Microsoft.P​​ractices.Unity.ResolutionFailedException的异常出现在Microsoft.P​​ractices.Unity.dll但在用户code没有处理


我可以看到有检查一个类型已注册的方法,但是当我做了检查,我怎么强制框架使用默认控制器啄?

 如果(container.IsRegistered(controllerType))

下面是我的路由,你可以看到......我想登录页面是第一页的人看到。

 公共类RouteConfig
{
    公共静态无效的RegisterRoutes(RouteCollection路线)
    {
        routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);        routes.MapRoute(
            名称:默认,
            网址:{控制器} / {行动} /,
            默认:新{
                 控制器=账户,
                 行动=登录,
                 RETURNURL = UrlParameter.Optional}
        );
    }
}


解决方案

实现在工厂略有不同的尝试:

 公共类UnityControllerFactory:DefaultControllerFactory
{
    私人IUnityContainer容器;
    私人IControllerFactory defaultControllerFactory;    公共UnityControllerFactory()
    {
        集装箱=新UnityContainer();
        defaultControllerFactory =新DefaultControllerFactory();
        RegisterTypes();
    }    公众覆盖一个IController CreateController(CTX的RequestContext,串controllerName)
    {
        尝试
        {
            返回container.Resolve<一个IController>(controllerName);
        }
        抓住
        {
            返回defaultControllerFactory.CreateController(CTX,controllerName);
        }
    }    私人无效RegisterTypes()
    {
        container.RegisterType< IUserRepository,EFUserRepository>();
    }
}

虽然仍踢了错误...嗯。

First time with Unity,

I have set up a class like this and registered in global.asax:

public class UnityControllerFactory : DefaultControllerFactory
{
    private IUnityContainer container;

    public UnityControllerFactory()
    {
        container = new UnityContainer();
        RegisterTypes();
    }

    protected override IController GetControllerInstance(
      System.Web.Routing.RequestContext requestContext, 
      Type controllerType)
    {
        return controllerType == null ?
            null :
            (IController)container.Resolve(controllerType);
    }

    private void RegisterTypes()
    {
        container.RegisterType<IUserRepository, EFUserRepository>();
    }
}

Problem is, when the AccountController (default MVC project comes with) is called, it throws an error:

An exception of type 'Microsoft.Practices.Unity.ResolutionFailedException' occurred in Microsoft.Practices.Unity.dll but was not handled in user code

I can see there is a method to check if a type has been registered, but when I have made that check, how do I force the framework to use the default controller thingy?

if (container.IsRegistered(controllerType))

Here is my routing, as you can see... I want the login page to be the first page people see..

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/",
            defaults: new {
                 controller = "Account",
                 action = "Login",
                 returnUrl = UrlParameter.Optional }
        );
    }
}

解决方案

Implemented a slightly different attempt at the factory:

public class UnityControllerFactory : DefaultControllerFactory
{
    private IUnityContainer container;
    private IControllerFactory defaultControllerFactory;

    public UnityControllerFactory()
    {
        container = new UnityContainer();
        defaultControllerFactory = new DefaultControllerFactory();
        RegisterTypes();
    }

    public override IController CreateController(RequestContext ctx, string controllerName)
    {
        try
        {
            return container.Resolve<IController>(controllerName);
        }
        catch 
        {
            return defaultControllerFactory.CreateController(ctx, controllerName);
        }
    }

    private void RegisterTypes()
    {
        container.RegisterType<IUserRepository, EFUserRepository>();
    }
}

still kicking up errors though... hmm.

这篇关于使用默认的控制器容器,如果统一注册的类型还没有得到确定这个电流控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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