Question

I made a change to my routing map which is following now:

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

And this is the actionlinks that are not working anymore:

@Html.ActionLink("Category", "CategoryList", "Category")

When I click on this actionlink nothing happens the url stays the same http://localhost:62394/ as the page reloads. its so weird

and when I check the html it looks like this:

<a href="">Category</a>

Any kind of help or tips is appreciate alot!

note: If I remove title from the routing it works...

Was it helpful?

Solution

It's because you've got 2 optional parameters so the routing engine doesn't know which one to map the third parameter to. It feels like title is going to be used for a specific route rather than a generic one. If that's the case, why not create a specific route for it and remove it from the generic fallback route?

Something like this:

routes.MapRoute(
            name: "Title",
            url: "Category-List/{title}",
            defaults: new { controller = "Category", action = "CategoryList", title = UrlParameter.Optional }
        );
routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top