lloydphillips
lloydphillips

Reputation: 2855

Why are my routing urls not matching in different controllers?

I've just created a SiteMap controller which generates a sitemap for my site. I'm trying to output the url of the products that I have listed throughout the site. In my products list view for example I have this code:

Url.Action("Details", "Products", new { id = item.Id, productName = Url.ToFriendlyUrl(item.Brand + " " + item.Colour + " " + item.Size + " " + item.Title) })

which outputs this url:

/products/details/1330/sandee-blue-small-camo-shorts

which maps to this route:

    routes.MapRoute(

        "ViewProduct",

        "products/{cat}/{id}/{productName}",

        new { controller = "products", action = "Details", cat = "", id = "", productName = "" },

        new string[] { "EliteFightKit.Web.Controllers" }

    );

In my SiteMap controller however I'm using this code to generate the url:

Url.Action("Details", "Products", new { id = item.Id, productName = Url.ToFriendlyUrl(item.Brand + " " + item.Colour + " " + item.Size + " " + item.Title) })

and it's outputting this:

/products/details/1330?productname=sandee-blue-small-camo-shorts

where am I going wrong, how do I get the SiteMap url to correlate with the url created in my products list view?

Lloyd

Upvotes: 0

Views: 109

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039140

Your route is not valid. Optional parameters can only appear at the end. In your case you have the cat route parameter which is option and which is in the middle of the url. So you need to provide a value for this parameter if you want this route to be picked instead of the default one:

Url.Action("Details", "Products", 
    new { id = "123", productName = "foo", cat = "bar" })

Upvotes: 1

Related Questions