0

我有以下方法:的Web API - 通过双参数,以获得方法

[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)}")] 

...但它也不起作用。

我该如何解决?

回答

5

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

此外,它看起来像您可以注册一个自定义路由,当使用内置助手生成链接时,自动添加尾部斜线到URL的末尾。

他最简单的解决方案是将下面一行添加到您的RouteCollection中。不知道你将如何与属性做到这一点,但在你的RouteConfig,你只补充一点:

routes.AppendTrailingSlash = true; 

有关详情,请here

最后所有的最大值和最小值功能适用于整数

最大:比最大的整数。 {x:max(10)}

我认为它不适用于双倍。

+0

问题的一半已解决,但不适用于范围限制。我创建了另一个问题:http://stackoverflow.com/questions/36119037/webapi-min-max-double-for-routeattribute –