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

查看:23
本文介绍了为什么我的带有双参数的 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 - 未找到您要查找的资源已被删除、更名或暂时不可用."

相关方法运行良好(此控制器上的其他方法).

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

推荐答案

我能够在我的机器上重现这个.

I was able to reproduce this on my machine.

只需在 URL 末尾添加一个/即可为我纠正它.看起来路由引擎将其视为 .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天全站免登陆