views:

67

answers:

2

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).

+3  A: 
routes.MapRoute(
    "person_id", 
    "person/{unnecessaryinfo}-{id}",
    new { controller = "Customer", action = "Details" },
    new { id = @"\d" }
);

You might also add some regex constraint in the unnecessaryinfo route token.

Darin Dimitrov
I've tried exactly what you have above and it still gives me "unnecessaryinfo-id" at the controller. I also don't understand what you mean by "You might also add some regex constraint in the unnecessaryinfo route token."
rkgallegos
What url do you use? `~/customer/details/foobarunnecessary-5` and `public ActionResult Details(string id)` will give you `id = 5` in this controller action. Also note that `\d` means a **single** digit. If you use `~/customer/details/foobarunnecessary-123` and still have the default route at the end (`{controller}/{action}/{id}`), then this default route will match and not your custom route and you will get `id = foobarunnecessary-123` in your controller action.
Darin Dimitrov
The end url we're trying to get to is ~/Person/unnecessaryinfo-42 and the number of digits will vary and can range from 1 to 6 or more.
rkgallegos
In this case adjust your regular expression: `id = @"[\d]{1,6}"`
Darin Dimitrov
Okay, here's what I have now routes.MapRoute("person_id", "person/{unnecessaryinfo}-{id}", new { controller = "Customer", action = "Details" }, new { id = @"[\d]{1,6}" }); and I'm still getting "unnecessaryinfo-1234" at the controller as the value for id.
rkgallegos
Edited the question to provide the latest code and a little more information.
rkgallegos
+5  A: 

Your back and forth on Darin's answer makes me believe that you probably have another route which is being matched and used instead of this route:

routes.MapRoute("person_id", "person/{unnecessaryinfo}-{id}",
    new { controller = "Customer", action = "Details" },
    new { id = @"[\d]{1,6}" });

Switching the order in which the routes are added to the RouteCollection should fix the problem. More specific routes should be the first added to the collection. For instance if you also have this route:

routes.MapRoute("person_login", "person/{id}",
    new { controller = "Customer", action = "Details" });

It should appear after your more specific "person_id" route so your code would look like this:

routes.MapRoute("person_id", "person/{unnecessaryinfo}-{id}",
    new { controller = "Customer", action = "Details" },
    new { id = @"[\d]{1,6}" });

routes.MapRoute("person_login", "person/{id}",
    new { controller = "Customer", action = "Details" });

The URL your provided 'http://example.com/Person/unnecessaryinfo-42' will match to both routes, but since your more specific route appears first that's the route that will be used. When a less specific URL is mapped it won't match the first and therefore the second route will be used.

That said determining what routes a URL matches can be a bit tricky. Thankfully, there are two pretty good options that assist with route debugging:

Stephen's solution is probably the best for longterm use, but Phil's is definitely the easier to implement for down and dirty debugging. I'd start with reordering your routes, but if that doesn't solve the issue use one of the aforementioned debuggers.

ahsteele