Web API - 将双参数传递给 GET 方法

Web API - pass double parameters to GET method

我有以下方法:

[Route("GetEditorialRequestsByCoordinates/{lat:min(-90):max(90)}/{lng:min(-180):max(180)}")]
[AutomapperExceptionApiFilterAttribute]
public HttpResponseMessage GetEditorialRequestsByCoordinates(double lat, double lng)
{

}

当我调用...时它工作正常

GET /v1/api/request/GetEditorialRequestsByCoordinates/48/2

但我想像这样传递双精度值:

GET /v1/api/request/GetEditorialRequestsByCoordinates/48.999/2.777

我遇到错误(404 未找到)。似乎,它无法通过路由找到合适的方法。 我尝试通过方式设置路线:

[Route("GetEditorialRequestsByCoordinates/{lat:double:min(-90):max(90)}/{lng:double:min(-180):max(180)}")]

...但它也不起作用。

我该如何解决?

只需在 URL 的末尾添加一个 / 即可为我更正它。看起来路由引擎将其视为 2.777 文件扩展名,而不是输入参数。

此外,您似乎可以注册一个自定义路由,在使用内置助手生成 link 时,它会自动在 URL 的末尾添加尾部斜杠。

最简单的解决方案是将以下行添加到您的 RouteCollection。不确定如何使用该属性,但在您的 RouteConfig 中,您只需添加:

routes.AppendTrailingSlash = true;

有关详细信息,请查看 here

所有最大和最小函数中的最后一个适用于整数

max:匹配一个有最大值的整数。 {x:max(10)}

我认为它不适用于 double。