2015-06-24 105 views
0

我正在使用第三方API和Castle Windsor的默認控制器工廠。不幸的是,這個第三方API有一些他們正在使用的控制器正在被Castle Windsor以外的其他實例化。所以基本上在我的方法中,我需要說,忽略這些控制器/路線,我該怎麼做?如何讓溫莎城堡忽略某些路線/控制器?

這裏是我廠:

public class WindsorControllerFactory : DefaultControllerFactory 
{ 
    private readonly IKernel _kernel; 

    public WindsorControllerFactory(IKernel kernel) 
    { 
     _kernel = kernel; 
    } 

    public override void ReleaseController(IController controller) 
    { 
     _kernel.ReleaseComponent(controller); 
    } 

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) 
    { 
     if (controllerType == null) 
      throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path)); 

     return (IController)_kernel.Resolve(controllerType); 
    } 
} 
+0

Castle Windsor ?? – inorganik

+0

您可以在GetControllerInstance中添加一個支票,該支票按類型忽略控制器.. – stuartd

回答

2

我猜你是按約定註冊的組件。在這種情況下,只需變更登記,使符合第三方的API,控制器不通過使用Unless條款

container.Register(
    Classes 
    .FromAssemblyInThisApplication() 
    .InSameNamespaceAs<Controller>() 
    .Unless(type => type.Name == "NotThisController" || type.Namespace.Contains("NotHere")) 
    .WithServiceAllInterfaces()); 

這是你的命名空間,類和結構的精心組織可以還清:)

註冊

編輯添加:stuartd的評論讓我意識到我沒有明確解釋下一步。然後在您的工廠檢查是否存在型號註冊和路由到正確的分辨率機制:

protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) 
{ 
    if (controllerType == null) 
     throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path)); 

    if (!_kernel.HasComponent(controllerType)) 
    { 
     // return the component with your custom third party resolution 
    } 

    return (IController)_kernel.Resolve(controllerType); 
} 
+0

..但是OP使用的是WindsorControllerFactory,所以即使未註冊,控制器仍會從Castle請求。 – stuartd

+0

@stuartd謝謝你的提醒,我簡直忘了第二部分:) – samy