Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC 3 Route resolution question
    primarykey
    data
    text
    <p>I have the following 3 routes</p> <ul> <li>student/available/classes/{id}</li> <li>student/available/classes/{studentId}/{classTypeId}</li> <li>student/available/classes/query/{q}</li> </ul> <p>This is how I register the routes in my global.asax.cs file</p> <pre><code>//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" }); </code></pre> <p>When I execute this url</p> <blockquote> <p>student/available/classes/query/smith+john</p> </blockquote> <p>MVC tries to run this route: </p> <blockquote> <p>student/available/classes/{studentId}/{classTypeId}</p> </blockquote> <p>If I reverse the order in which I register the Query route with the GetClassForStudent route, MVC resolves to the query route. </p> <p>What is going on here, and how can I register these routes with MVC so that they all resolve correctly?</p> <p><strong>UPDATE</strong></p> <p>Wow, once again thank you to everyone here on stackoverflow! Based on everyone's responses, and in particular the answer by Beno, I now understand my issue, and was able to make it work! </p> <p>From what I understand, I was not giving MVC enough information about the routes. It was matching the word 'query' into the {studentId} parameter. From Beno's answer I learned about parameter constraints. So now I am able to tell MVC to expect a Guid type in the {studentId} (and {customerId}) parameter.</p> <p>Here is the code now.</p> <pre><code> //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" }); </code></pre> <p>The class <a href="https://stackoverflow.com/questions/2421995/how-can-i-create-a-route-constraint-of-type-system-guid">GuidConstraint I found from this stackoverflow question</a>.</p> <p>Thank you!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload