似乎没有控制器我的控制器工厂认可

时间:2017-03-07 13:53:35

标签: c# asp.net-mvc autofac controller-factory

我想用Autofac创建一个controllerfactory,但是当我运行我的项目时,我在这一行中得到了空参考实例错误:string ctrName = contr.ControllerContext.RouteData.Values["Controller"].ToString()

这是我的controller factory

internal class ControllerFactory : DefaultControllerFactory
{
    private readonly Dictionary<string, Func<RequestContext, IController>> _controllerMap;

    public ControllerFactory()
    {
        List<IController> lstControllers = DependencyResolver.Current.GetServices<IController>().ToList();
        _controllerMap = new Dictionary<string, Func<RequestContext, IController>>();

        foreach (Controller contr in lstControllers)
        {
            string ctrName = contr.ControllerContext.RouteData.Values["Controller"].ToString();  //iv got null instance error in this line
            _controllerMap.Add(ctrName, c => contr);
        }
    }

    public override IController CreateController(RequestContext requestContext, string controllerName)
    {

        if (_controllerMap.ContainsKey(controllerName))
        {
            return _controllerMap[controllerName](requestContext);
        }
        else
            throw new KeyNotFoundException(controllerName);
    }
}

这是我的appstart:

        protected void Application_Start()
    {

        var builder = new ContainerBuilder();

        builder.RegisterControllers(typeof(MvcApplication).Assembly).AsImplementedInterfaces();

        builder.Register(s => new ControllerFactory()).As<IControllerFactory>();


        var container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));


        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

错误原因很清楚:我的foreach项目的ControllerContext为空...但为什么?我的解决方案中有控制器:

   public class HomeController : Controller 
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult About()
    {
        ViewBag.Message = "Your application description page.";

        return View();
    }

    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";

        return View();
    }

}

更新:我检查了foreach上的调试器..项类型为WebApplication1.Controllers.HomeController表示我的家庭控制器,但mt项的控制器上下文为空!!!

enter image description here

1 个答案:

答案 0 :(得分:1)

  

这意味着您可以访问的最早点   ControllerContext属性是通过覆盖Execute或Initialize来实现的   方法(但是先调用base.Execute或base.Initialize):

protected override void Execute(RequestContext requestContext)
{
  base.Execute(requestContext);

  // .ControllerContext is available from this point forward.
}

protected override void Initialize(RequestContext requestContext)
{
  base.Initialize(requestContext);

  // .ControllerContext is available from this point forward.
}
  

后者(初始化)是您最早的最早点   可以使用ControllerContext属性,除非你处理了   自己分配,不推荐(作为部分   框架将取决于将该属性分配给   那个时候)。

来源:Null ControllerContext in my custom controller inheriting from BaseController

根据这些新信息,您必须map {/ 1}} {/ 1}}。