如何支持同时处理 GUID 和整数的 GET
路径? 我知道 GUID 不是最理想的选择,但目前它就是这样。我想添加对整数的支持,以便用户可以更轻松地记住并传达应该是唯一“键”的内容。
示例路径:
testcases/9D9A691A-AE95-45A4-A423-08DD1A69D0D1
testcases/1234
我的WebApiConfig
:
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
var routes = config.Routes;
routes.MapHttpRoute("DefaultApiWithAction",
"Api/{controller}/{action}");
routes.MapHttpRoute("DefaultApiWithKey",
"Api/{controller}/{key}",
new { action = "Get" },
new { httpMethod = new HttpMethodConstraint(HttpMethod.Get), key = @"^\d+$" });
routes.MapHttpRoute("DefaultApiWithId",
"Api/{controller}/{id}",
new { action = "Get" },
new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) });
routes.MapHttpRoute("DefaultApiGet",
"Api/{controller}",
new { action = "Get" },
new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) });
routes.MapHttpRoute("DefaultApiPost",
"Api/{controller}",
new { action = "Post" },
new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) });
}
我的控制器(仅方法签名):
[RoutePrefix("Api/TestCases")]
public class TestCasesController : PlanControllerBase
{
[Route("")]
public OperationResult<IEnumerable<TestCaseDTO>> Get([FromUri] TestCaseRequest request)
[Route("{id}")]
[HttpGet]
public OperationResult<TestCaseDTO> Get(Guid id)
[Route("{key}")]
[HttpGet]
public OperationResult<TestCaseDTO> Get(int key)
...
}
当我尝试使用整数调用资源时,出现了内部服务器错误。感谢任何帮助!