为什么有双ARGS我的Web

为什么有双ARGS我的Web

本文介绍了为什么有双ARGS我的Web API方法没有得到所谓的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个双ARGS一个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的末尾纠正对我来说。看起来象路由引擎正在观看它作为一个0.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在每个网址年底

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;

这篇关于为什么有双ARGS我的Web API方法没有得到所谓的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 08:31