0

Here is my RouteConfig.cs

routes.MapRoute(null,
                        "{controller}/Page{page}",
                        new {controller = "Product", action = "Index", category = (string) null},
                        new {page = @"\d+"}
            );

        routes.MapRoute(null,
                        "{controller}/{category}",
                        new {controller = "Product", action = "Index", page = 1}
            );

        routes.MapRoute(null,
                        "{controller}/{category}/Page{page}",
                        new {controller = "Product", action = "Index"},
                        new {page = @"\d+"}
            );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

And here is the code for generating url:

@Html.ActionLink("View Cart", "Index", "ShoppingCart", null, new { @class = "btn btn-orange" })

It works well when I navigate to, for example, Product/Page2, Product/Laptop, Product/Laptop/Page2. The problem is, whenever my current URL contains Page segment, it will try to reuse that segment for generating outgoing URL. So, if I'm at Product/Page2 the above generated URL would be ShoppingCart/Page2. I don't know how to avoid this.

Please help me. Thank you so much.

EDIT!!!

I've found a workaround way. Instead of using ActionLink, I use RouteLink like this:

@Html.RouteLink("View Cart", "Default", new { controller = "ShoppingCart", action = "Index" }, new { @class = "btn btn-orange" })

But I still want to use ActionLink, so please help me.

EDIT!!!

The workaround way doesn't work when I generate a link to ShoppingCart/Checkout. It still take me to Index action in ShoppingCart controller.

7
  • For the shopping cart link, which route are you targeting? Commented May 26, 2013 at 7:29
  • The last one, just the normal pattern, controller/action. Commented May 26, 2013 at 8:19
  • but it matches in the second route itself. You need to narrow the second mapping with more constraints or rearrange the mappings and see if it helps. Commented May 26, 2013 at 9:30
  • What should I do now? The second one is for the category of the product. When I change all {controller} into Product, everything works perfectly. Is that the solution? Commented May 26, 2013 at 9:36
  • If that route target only the Product Controller, that is the way to go. Commented May 26, 2013 at 9:44

2 Answers 2

1

Create a new route pattern specific to ShoppingCart and make it the first route by placing it at the TOP.

    routes.MapRoute(null,
                    "ShoppingCart/{action}",
                    new {controller = "Product"});
        );

As a rule all the specific routes should come first.

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

Comments

0

This is because of the way the routing system tries to evaluate the value of segment variables when trying to match against a route.

So when the call to render the link occurs with the following arguments:

@Html.ActionLink("View Cart", "Index", "ShoppingCart", null, new { @class = "btn btn-orange" })

the framework when evaluating the route with template

{controller}/Page{page}

will resolve the controller segment variable to be ShoppingCart however when it cannot find a value for the page segment variable (via any of the arguments in the method call), it will then try and resolve that value from the RouteData object in the ViewContext. Since you have navigated to Product/Page2, the current value of page within the routes value dictionary is 2.

You can inspect this by looking at the value of ViewContext.RouteData.Values["page"] when rendering that view.

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.