I'm having some trouble trying to configure my ASP.NET MVC project to route multiple URLs to the same view. Given the following URLs:
localhost:1234
localhost:1234/Products
localhost:1234/Products/1
localhost:1234/Products/abcd
localhost:1234/Products/whatever
I would like each of these to route the user to the same view (Products.cshtml, for instance).
Following an example on this site, I've decorated my Controller action with a special route attribute:
[HttpGet]
[Route("Products/{id?}")]
public ActionResult Products(string id)
{
return View();
}
And in my RouteConfig.cs file, I have my default route set up:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Products", action = "Products", id = UrlParameter.Optional }
);
The localhost:1234 and the localhost:1234/Products links both work, but this isn't working for the remaining URLs.
MapMvcAttributeRoutes()somewhere in your initialization code?url: "Products/{id}",(with the same defaults) before the default route and remove the[Route("Products/{id?}")]attributeroutes.MapRoutedeclaration with a blank URL parameter. Could you post your comment as an answer?