【问题标题】:Navigation Routes Controller issue in Twitter.Bootstrap.MVC4 Nuget packageTwitter.Bootstrap.MVC4 Nuget 包中的导航路由控制器问题
【发布时间】:2013-04-23 10:32:07
【问题描述】:

使用 Twitter.Bootstrap.MVC4 是否可以将“null”传递给 ExampleLayoursRoute.config 中的 客户控制器

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapNavigationRoute<HomeController>("Home Page", c => c.Index());

        routes.MapNavigationRoute<CustomerController>("Customer", null)  <-- pass null here
              .AddChildRoute<CustomerController>("List", c => c.Index())
              .AddChildRoute<CustomerController>("Add", c => c.Create())
            ;
    }

我收到一个错误:对象引用未设置为 NavigationRouteconfigureationExtensions.cs 文件中的对象实例:

  public static NamedRoute ToDefaultAction<T>(this NamedRoute route, Expression<Func<T, ActionResult>> action,string areaName) where T : IController
    {
        var body = action.Body as MethodCallExpression; <--- Error here

您不能添加指向同一个控制器/动作的链接:

        routes.MapNavigationRoute<CustomerController>("Customer", c => c.Index())
              .AddChildRoute<CustomerController>("List", c => c.Index())

或者您收到错误:{“名为 'Navigation-Customer-Index' 的路由已在路由集合中。路由名称必须是唯一的。\r\n参数名称:名称”}

到目前为止,我唯一的解决方法是在控制器中添加第二个重复的 Action,并将其命名为 Index2(例如):

public ActionResult Index()
    {
        return View(db.Customers.Where(x => x.UserName == User.Identity.Name).ToList());
    }

 public ActionResult Index2()
    {
        return View(db.Customers.Where(x => x.UserName == User.Identity.Name).ToList());
    }

有没有比复制代码或添加不必要的操作更好的方法?

谢谢,马克

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 twitter-bootstrap


    【解决方案1】:

    我发现问题出在 Global.asax 文件中的语句:

        BootstrapSupport.BootstrapBundleConfig.RegisterBundles(System.Web.Optimization.BundleTable.Bundles);
            BootstrapMvcSample.ExampleLayoutsRouteConfig.RegisterRoutes(RouteTable.Routes);
            BootstrapSupport.BootstrapBundleConfig.RegisterBundles(System.Web.Optimization.BundleTable.Bundles);
            BootstrapMvcSample.ExampleLayoutsRouteConfig.RegisterRoutes(RouteTable.Routes);
    

    由于安装 1.09 和卸载 Twitter Bootstrap Nuget 包,我发现 Global.asax 文件中的条目重复。删除这些重复条目有效。这 2 个调用至少需要一个条目。

    【讨论】:

      【解决方案2】:

      转到 NavigationRouteConfigurationExtension.cs。找到比这更好的方法,但是这个 hack 应该使它起作用(它只是一个证明)。问题是添加两个同名路由,并且名称是从路由生成的,而不是显示名称。

          public static NavigationRouteBuilder AddChildRoute<T>(this NavigationRouteBuilder builder, string DisplayText, Expression<Func<T, ActionResult>> action,string areaName="") where T : IController
          {
              var childRoute = new NamedRoute("", "", new MvcRouteHandler());
              childRoute.ToDefaultAction<T>(action,areaName);
              childRoute.DisplayName = DisplayText;
              childRoute.IsChild = true;
              builder._parent.Children.Add(childRoute);
      
              //builder._routes.Add(childRoute.Name,childRoute);
              builder._routes.Add(Guid.NewGuid().ToString(), childRoute);
      
              return builder;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-16
        • 2012-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多