Search code examples
c#asp.net-mvcasp.net-mvc-routing

MVC routing expect the Id when it sould not


I have troubles with my routing: I have in my controller this method:

[HttpGet]
public JsonResult GetCase(CustomIdentity currentUser, string objectType, Guid objectId)

And in my routes

r.Match("CTRL/{id}", "CTRL", "details");
r.Match("CTRL/GetCase", "CTRL", "GetCase");

The problem is when i want to initiate a GET to my method: I can do that only with

http://a.com/CTRL/GetCase/EE5014C2-C4AA-44E2-80F9-A23D01317790?objectType=123&objectId=1E5014C2-C4AA-44E2-80F9-A23D01317790

But i need

http://a.com/CTRL/GetCase?objectType=123&objectId=1E5014C2-C4AA-44E2-80F9-A23D01317790

What is wrong with my code?


Solution

  • Switch the order of the route setup. The first URI...

    http://a.com/CTRL/GetCase/EE5014C2-C4AA-44E2-80F9-A23D01317790?objectType=123&objectId=1E5014C2-C4AA-44E2-80F9-A23D01317790
    

    matches the CTRL/{id} route you set up where GetCase in the URI satisfies {id} template parameter. The convention uses the first matched route it finds when mapping routes.

    You need to change the order you set up the route. From what you've shown, that would be

    r.Match("CTRL/GetCase", "CTRL", "GetCase");
    r.Match("CTRL/{id}", "CTRL", "details");