MVC 路由在不应该的时候期望 Id

MVC routing expect the Id when it sould not

我的路由有问题: 我的控制器中有这个方法:

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

在我的路线中

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

问题是当我想对我的方法启动 GET 时: 我只能用

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

但我需要

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

我的代码有什么问题?

切换路由设置的顺序。第一个 URI...

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

匹配您设置的 CTRL/{id} 路由,其中​​ URI 中的 GetCase 满足 {id} 模板参数。约定使用它在映射路由时找到的第一个匹配路由。

您需要更改设置路线的顺序。根据您展示的内容,那将是

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