5

Using the Twitter.Bootstrap.MVC4 is it possible to pass "null" to the Customer controller in the 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())
            ;
    }

I get an error: Object reference not set to an instance of an object in the NavigationRouteconfigureationExtensions.cs file:

  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

You can't add a link to the same controller/action:

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

Or you get the error: {"A route named 'Navigation-Customer-Index' is already in the route collection. Route names must be unique.\r\nParameter name: name"}

My only workaround so far, is to add a second duplicate Action in the controller, and name it Index2 (for example):

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());
    }

Is there a better way, than duplicating code, or adding unnecessary actions?

Thanks, Mark

2 Answers 2

5

I found the problem to be the statements in Global.asax file:

    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);

Due to installing 1.09 and uninstalling the Twitter Bootstrap Nuget package, I found the entries in Global.asax file to be repeated. Removing these duplicate entries worked. You need at least one entry for these 2 calls.

Sign up to request clarification or add additional context in comments.

Comments

0

Go to NavigationRouteConfigurationExtension.cs. Find a better way than this, but this hack should make it work (its just a proof). Problem is adding two routes with the same name and the name is generated from route, not display name.

    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;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.