Question

I need a little help on what i'm trying to achieve. I'm doing an ASP MVC App and i can't make the routes properly.

I'm using the nuget package Attribute Routing (which is now by default in MVC5).

First I hadn't any issues using it all the routes where matching properly the right things, but then I started working on Internationalization, and everything blew up !

So here's the problem :

I need to get those urls : domain/culture/id/product/action but I only have one Action for my products. So when the user comes to my website, it sets the language then it redirects him to the product he asked (or the default one if none asked)

I tried

return RedirectToRoute("/" + cultureName + "/" + id + "/" + productName + "/");

But it didn't work telling me that the route doesn't exist which is impossible because in my Routing Explorer I clearly see the route /{culture}/{id}/ProductName

Is it possible to do this, this way or do i need to change my way of thinking about the routing ?

Was it helpful?

Solution

return RedirectToRoute("SomeRouteName", new { 
    culture = cultureName,
    id = id, 
    productName = productName 
});

and your route definition:

routes.MapRoute(
    "SomeRouteName",
    "{culture}/{id}/{productName}",
    new { controller = "Home", action = "Index" }
);

Notice that the pattern of the route is {culture}/{id}/{productName} where you need to specify each token when calling RedirectToAction. Since you have only one action in this controller you don't need to specify it. It is defined in your route.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top