为什么我的带有双参数的 Web API 方法没有被调用?

编程入门 行业动态 更新时间:2024-10-25 12:21:09
本文介绍了为什么我的带有双参数的 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:

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.

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 方法没有被调用?

本文发布于:2023-11-03 15:56:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555508.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   方法   Web   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!