微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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一起使用,但请尝试一下。 ;-)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。