对工厂的第一次调用是正确的,controllerType“ProductsController”作为参数传入.但有时候,工厂被称为几次,视图被显示为一个空值为控制器,我不知道为什么.当控制器类型的正确值通过“调用堆栈”对我有意义,但是当通过null时,我不知道为什么或正在进行调用.有任何想法吗?
调用堆栈时工作
Test.DLL!Test.UnityHelpers.UnityControllerFactory.GetControllerInstance(System.Type controllerType = {Name =“ProductsController”FullName =“Test.Controllers.ProductsController”})行23 C#
Test.DLL!Test._Default.Page_Load(object sender = {ASP.default_aspx},System.EventArgs e = {System.EventArgs})行18 0x1a bytes C#
在controllerType传递NULL时调用堆栈
Test.DLL!Test.UnityHelpers.UnityControllerFactory.GetControllerInstance(System.Type controllerType = null)第27行C#
首先我创建了一个UnityControllerFactory
public class UnityControllerFactory : DefaultControllerFactory { UnityContainer container; public UnityControllerFactory(UnityContainer container) { this.container = container; } protected override IController GetControllerInstance(Type controllerType) { if (controllerType != null) { return container.Resolve(controllerType) as IController; } else { return null; // I never expect to get here,but I do sometimes,the callstack does not show the caller } } }
接下来,我添加了以下代码global.asax文件来实例化容器工厂
protected void Application_Start() { RegisterRoutes(RouteTable.Routes); // Create Unity Container if needed if (_container == null) { _container = new UnityContainer(); } // Instantiate a new factory IControllerFactory unityControllerFactory = new UnityControllerFactory(_container); // Register it with the MVC framework ControllerBuilder.Current.SetControllerFactory(unityControllerFactory); // Register the sqlProductRepository _container.RegisterType<IProductsRepository,sqlProductRepository> (new ContainerControlledLifetimeManager()); }
该应用程序有一个控制器
public class ProductsController : Controller { public IProductsRepository productsRepository; public ProductsController([Dependency]IProductsRepository productsRepository) { this.productsRepository = productsRepository; } }
解决方法
但这并不是唯一可能发生的原因,但它是常见的.
适当的做法是在这些情况下允许base方法处理请求.它通常只是一个简单的文件请求,不应该对您有任何影响.
最简单的事情就是把它当成这样:
if (controllerType != null) { return container.Resolve(controllerType) as IController; } else { return base.GetControllerInstance(requestContext,controllerType); }
应该这样做
要查看请求是什么,您可以检查HttpContext.Current.Request以查看路由中没有哪个文件.很多时候,这不是你要控制的东西,但它会让你更好地了解请求的来源.