Reputation: 13594
I am using the following routing in my project
routes.MapRoute(
"ProductRoute", // Route name
"product/{id}/{title}", // URL with parameters
new { controller = "product", action = "Index", id = UrlParameter.Optional }, // Parameters defaults
new[] { "PriceCompare.Controllers" }
);
The problem at hand is how the url is displayed in return. One can access the URL in any of the following ways:
All is fine, as all these URLs redirect to the desired place. But, what i think would be nice is even if someone uses the 2nd or 3rd approach, the return URL in browser should show the 1st URI.
Just like StackOverflow. For example if you visit the following URL
stackoverflow.com/questions/734249/, your browser address will show the complete URL in browser stackoverflow.com/questions/734249/asp-net-mvc-url-routing-with-multiple-route-values
How can this be achieved?
Upvotes: 1
Views: 773
Reputation: 23113
You can either implement your own Route
or do something like this in your action:
public ActionResult Index(int? id, string title = null)
{
if (String.IsNullOrWhiteSpace(title))
{
var product = // load product
return Redirect(Url.Action("Index", "Product",
new { id = id, title = product.Title }));
}
// your code
}
Upvotes: 2