为什么我的Web API方法与双引号没有被调用? [英] Why is my Web API method with double args not getting called?

查看:224
本文介绍了为什么我的Web API方法与双引号没有被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web API方法,需要两个双重参数:

I have a Web API method that takes two double args:

资源库界面:

public interface IInventoryItemRepository
{
. . .
    IEnumerable<InventoryItem> GetDepartmentRange(double deptBegin, double deptEnd);
. . .
}

资料库:

public IEnumerable<InventoryItem> GetDepartmentRange(double deptBegin, double deptEnd)
{
    // Break the doubles into their component parts:
    int deptStartWhole = (int)Math.Truncate(deptBegin);
    int startFraction = (int)((deptBegin - deptStartWhole) * 100);
    int deptEndWhole = (int)Math.Truncate(deptEnd);
    int endFraction = (int)((deptBegin - deptEndWhole) * 100);

    return inventoryItems.Where(d => d.dept >= deptStartWhole).Where(e => e.subdept >= startFraction)
        .Where(f => f.dept <= deptEndWhole).Where(g => g.subdept >= endFraction);
}

控制器:

[Route("api/InventoryItems/GetDeptRange/{BeginDept:double}/{EndDept:double}")]
public IEnumerable<InventoryItem> GetInventoryByDeptRange(double BeginDept, double EndDept)
{
    return _inventoryItemRepository.GetDepartmentRange(BeginDept, EndDept);
}

当我尝试调用此方法时,通过:

When I try to invoke this method, via:

http://localhost:28642/api/inventoryitems/GetDeptRange/1.1/99.99

...我得到, HTTP错误404.0 - 未找到
您要查找的资源已被删除,名称已更改,或暂时无法使用。

...I get, "HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."

相关方法运行正常(本控制器上的其他方法)。

The related methods run fine (other methods on this Controller).

推荐答案

我可以在我的机器上重现这个。

I was able to reproduce this on my machine.

只需在网址的末尾添加一个/我。看起来路由引擎正在以.99文件扩展名而不是输入参数来查看它。

Simply adding a / to the end of the URL corrected it for me. Looks like the routing engine is viewing it as a .99 file extension, rather than an input parameter.

http://localhost:28642/api/inventoryitems/GetDeptRange/1.1/99.99/

另外,看起来你可以注册在使用内置助手生成链接时,自定义路径会自动将尾随斜线添加到URL的末尾。我没有亲自测试过:
stackoverflow在每个URL的末尾添加尾部斜杠

Additionally, it looks like you can register a custom route that automatically adds a trailing slash to the end of the URL when using the built-in helpers to generate the link. I have not tested this personally: stackoverflow Add a trailing slash at the end of each url

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

The easiest solution is to just add the following line to your RouteCollection. Not sure how you would do it with the attribute, but in your RouteConfig, you just add this:

routes.AppendTrailingSlash = true;

这篇关于为什么我的Web API方法与双引号没有被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆