We are trying to use custom routes in an ASP.NET MVC application to generate this url: 'http://example.com/Person/unnecessaryinfo-42'
. The "unnecessaryinfo" is the name of the id which will make the URL hackable for the user. Below is the code for our route map. This works but my controller action ends up with "unnecesaryinfo-42" in the id parameter. Can I strip the "unnecessaryinfo-" before it reaches the controller action?
routes.MapRoute("person_id", "person/{id}",
new { controller = "Customer", action = "Details" },
new { id = @"unnecessaryinfo-\d" });
Edit:
Here's our latest code:
routes.MapRoute("person_id", "person/{unnecessaryinfo}-{id}",
new { controller = "Customer", action = "Details" },
new { id = @"[\d]{1,6}" });
The chapter on Routing from ASP.NET MVC 2 In Action (page 234) discusses avoiding the use of database IDs whenever possible but if you must use them, adding additional information to make the URL readable. This is what we're trying (unsuccessfully) to do. The database id currently ranges from 1 to 6 digits (and may grow over time).