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

Ole*_* Sh 2 asp.net-web-api asp.net-web-api-routing asp.net-web-api2

我有以下方法:

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

}
Run Code Online (Sandbox Code Playgroud)

我打电话时工作正常......

GET /v1/api/request/GetEditorialRequestsByCoordinates/48/2
Run Code Online (Sandbox Code Playgroud)

但是我希望像这样传递double值:

GET /v1/api/request/GetEditorialRequestsByCoordinates/48.999/2.777
Run Code Online (Sandbox Code Playgroud)

我收到了一个错误(找不到404).好像,它找不到合适的路线方法.我试图通过以下方式设置路线:

[Route("GetEditorialRequestsByCoordinates/{lat:double:min(-90):max(90)}/{lng:double:min(-180):max(180)}")]
Run Code Online (Sandbox Code Playgroud)

......但它也不起作用.

我该如何解决?

Ani*_*aha 7

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

此外,看起来您可以注册一个自定义路由,在使用内置帮助程序生成链接时,会自动在URL的末尾添加尾部斜杠.

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

routes.AppendTrailingSlash = true;
Run Code Online (Sandbox Code Playgroud)

有关详情,请点击这里.

最后一个max和min函数适用于整数

max:匹配具有最大值的整数.{X:最大(10)}

我认为它不适合双倍.