ASP.NET路由-末尾带有斜杠

问题描述

| 考虑以下服务合同:
[WebGet(UriTemplate = \"/stores\")]
DTO.Stores GetAllStores();

[WebGet(UriTemplate = \"/stores/{name}\")]
DTO.Stores GetStores(string name);
我可以到达以下两个网址:http:// localhost / v1 / stores和http:// localhost / v1 / stores / Joe。但是,URL http:// localhost / v1 / stores /(注意末尾的斜杠)向我返回一个“找不到端点”错误。理想情况下,我希望http:// localhost / v1 / stores /调用GetAllStores()。 我怎样才能做到这一点?谢谢!

解决方法

我会尝试插入波浪号。也许是“〜/ stores”? 或者,通过路由,将\“ / \”放在前面。,如果您使用\“ string?name \”作为参数怎么办?
[WebGet(UriTemplate = \"/stores/{name}\")]
DTO.Stores GetStores(string? name);
而且,由于两种方法都返回相同的内容(DTO.Stores),因此可以使用一种方法而不是两种方法来获取Stores(就像现在所做的那样)。像这样:
[WebGet(UriTemplate = \"/stores/{name}\")]
DTO.Stores GetStores(string? name)
{
    if(string.IsNullOrEmpty(name))
    {
        //get specific store
    }
    else
    {
        //get all stores
    }
}
附注:我不确定这是否可以与WCF一起使用,但请尝试一下。 ;-)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...