Twitter.Bootstrap.MVC4 Nuget包中的导航路由控制器问题

Mar*_*ark 5 asp.net-mvc asp.net-mvc-3 twitter-bootstrap

使用Twitter.Bootstrap.MVC4可以将"null"传递给ExampleLayoursRoute.config中的Customer控制器:

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())
            ;
    }
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:对象引用未设置为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
Run Code Online (Sandbox Code Playgroud)

您无法添加指向同一控制器/操作的链接:

        routes.MapNavigationRoute<CustomerController>("Customer", c => c.Index())
              .AddChildRoute<CustomerController>("List", c => c.Index())
Run Code Online (Sandbox Code Playgroud)

或者您收到错误:{"名为'Navigation-Customer-Index'的路径已经在路径集合中.路径名称必须是唯一的.\ r \nParameter name:name"}

到目前为止,我唯一的解决方法是在控制器中添加第二个重复的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());
    }
Run Code Online (Sandbox Code Playgroud)

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

谢谢,马克

小智 5

我发现问题是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);
Run Code Online (Sandbox Code Playgroud)

由于安装了1.09并卸载了Twitter Bootstrap Nuget包,我发现Global.asax文件中的条目要重复.删除这些重复的条目工作.这两个电话至少需要一个条目.


vvu*_*tic 0

转到 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;
    }
Run Code Online (Sandbox Code Playgroud)