自定义控制器,不适用于区域

时间:2018-07-28 21:45:30

标签: c# jquery asp.net asp.net-mvc controller

我的项目中有一个自定义控制器,因为我将“结构图”用作IOC容器。当我添加Datatables.mvc(jQuery网格库)并从区域调用视图时,自定义Controller在运行时无法正常工作,并给我一个错误。当我从引用中删除Datatables.mvc时,该项目将成功编译并呈现视图表单控制器。

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        setDbInitializer();
        //Set current Controller factory as StructureMapControllerFactory
        ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());
        Microsoft.AspNet.SignalR.GlobalHost.DependencyResolver = SmObjectFactory.Container.GetInstance<Microsoft.AspNet.SignalR.IDependencyResolver>();
    }
    private static void setDbInitializer()
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<OnlineAcademyDbContext, Configuration>());
        SmObjectFactory.Container.GetInstance<IUnitOfWork>().ForceDatabaseInitialize();
    }
    public class StructureMapControllerFactory : DefaultControllerFactory
    {
        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if (controllerType == null)
            {
                throw new HttpException(404, $"Resource not found : {requestContext.HttpContext.Request.Path}");
            }
            return SmObjectFactory.Container.GetInstance(controllerType) as Controller;
        }
    }
    protected void Application_EndRequest(object sender, EventArgs e)
    {
        HttpContextLifecycle.DisposeAndClearAll();
    }
}

这是当我从区域调用视图并且Datatable.mvc已添加到引用中时的错误:

  

System.Web.HttpException:找不到资源:区域的网址...   ControllerType为空

谢谢。

1 个答案:

答案 0 :(得分:0)

好吧。问题已经解决。解决方案是更换计算机。我认为iis或Os混淆了原因