Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this MVC route not working?

here are two routes from my global.asax file. I'm trying to go to the second route and I'm getting a default 404 resource not found error.

When i remove the first route (listed in this example), it works.

How can i fix this, please?

Snippet of global.asax code

// GET: /user/PureKrome/Alert/69
routes.MapRoute(
    "User-Alert-Details",
    "user/{displayName}/alert/{alertId}",
    new { controller = "Account", action = "AlertDetails", alertId = 0 });

// GET: /user/PureKrome/Alert/create
routes.MapRoute(
    "User-Alert-Create",
    "user/{displayName}/alert/create",
    new { controller = "Account", action = "AlertCreate" });
like image 371
Pure.Krome Avatar asked Mar 24 '09 12:03

Pure.Krome


1 Answers

Your first route is a "greedy" route and will happily accept "create" as the alertId in the last parameter. It appears that you intend the alertId parameter to be numeric only, so you should add a constraint to tell the route system that that last parameter must be numeric.

See this tutorial.

For example:

// GET: /user/PureKrome/Alert/69
routes.MapRoute(
    "User-Alert-Details",
    "user/{displayName}/alert/{alertId}",
    new { controller = "Account", action = "AlertDetails", alertId = 0 },
    new { alertId = @"\d+" });

// GET: /user/PureKrome/Alert/create
routes.MapRoute(
    "User-Alert-Create",
    "user/{displayName}/alert/create",
    new { controller = "Account", action = "AlertCreate" });

Note, you can also reverse the order of the routes, but even if you do, you should still include a constraint for correctness if you want alertId to always be a number.

like image 50
Erv Walter Avatar answered Nov 05 '22 20:11

Erv Walter