asp.net - MVC 3 Route resolution question -



asp.net - MVC 3 Route resolution question -

i have next 3 routes

student/available/classes/{id} student/available/classes/{studentid}/{classtypeid} student/available/classes/query/{q}

this how register routes in global.asax.cs file

//student checkin routes.maproute("studentavailableclasses_getallforstudent", "student/available/classes/{id}", new {controller = "studentcheckin", action = "getbyid"}); routes.maproute("studentavailableclasses_getclassforstudent", "student/available/classes/{studentid}/{classtypeid}", new { controller = "studentcheckin", action = "getbystudentandclasstype" }); routes.maproute("studentavailableclasses_query", "student/available/classes/query/{q}", new { controller = "studentcheckin", action = "query" });

when execute url

student/available/classes/query/smith+john

mvc tries run route:

student/available/classes/{studentid}/{classtypeid}

if reverse order in register query route getclassforstudent route, mvc resolves query route.

what going on here, , how can register these routes mvc resolve correctly?

update

wow, 1 time 1 time again give thanks here on stackoverflow! based on everyone's responses, , in particular reply beno, understand issue, , able create work!

from understand, not giving mvc plenty info routes. matching word 'query' {studentid} parameter. beno's reply learned parameter constraints. able tell mvc expect guid type in {studentid} (and {customerid}) parameter.

here code now.

//student checkin routes.maproute("studentavailableclasses_getallforstudent", "student/available/classes/{id}", new {controller = "studentcheckin", action = "getbyid"}, new {id = new guidconstraint()}); routes.maproute("studentavailableclasses_getclassforstudent", "student/available/classes/{studentid}/{classtypeid}", new {controller = "studentcheckin", action = "getbystudentandclasstype"}, new {studentid = new guidconstraint(), classtypeid = new guidconstraint()}); routes.maproute("studentavailableclasses_query", "student/available/classes/query/{q}", new { controller = "studentcheckin", action = "query" });

the class guidconstraint found stackoverflow question.

thank you!

what going on here?

the url, student/available/classes/query/smith+john correctly picked route student/available/classes/{studentid}/{classtypeid} since {studentid} can anything, including 'query'. 'smith+john' picked {classtypeid}

how can register these routes mvc resolve correctly?

you add together validation {studentid} field. don't know studentid if it's 8 digit number:

routes.maproute("studentavailableclasses_getclassforstudent", "student/available/classes/{studentid}/{classtypeid}", new { controller = "studentcheckin", action = "getbystudentandclasstype" } new { studentid = @"\d{8}" } );

or

you place studentavailableclasses_query route @ top gets matched before other one

or

a combination of both best bet

asp.net asp.net-mvc-3 c#-4.0 routes

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -