ASP.NET 路由 - 末尾有或没有斜杠
考虑以下服务合同:
[WebGet(UriTemplate = "/stores")]
DTO.Stores GetAllStores();
[WebGet(UriTemplate = "/stores/{name}")]
DTO.Stores GetStores(string name);
我可以访问这两个 URL: http://localhost/v1/stores 和 http://localhost/v1/stores/Joe。然而,Url http://localhost/v1/stores/ (注意末尾的斜杠)返回给我一个“找不到端点”错误。理想情况下,我希望 http://localhost/v1/stores/ 调用 GetAllStores()。
我怎样才能做到这一点?谢谢!
Considering the following Service Contract:
[WebGet(UriTemplate = "/stores")]
DTO.Stores GetAllStores();
[WebGet(UriTemplate = "/stores/{name}")]
DTO.Stores GetStores(string name);
I can reach these two Urls: http://localhost/v1/stores and http://localhost/v1/stores/Joe. However the Url http://localhost/v1/stores/ (notice the slash at the end) returns me an "Endpoint not found" error. Ideally, I would like http://localhost/v1/stores/ to call GetAllStores().
How can I do that? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会尝试输入波浪号。也许“~/stores”?
或者,使用路由时,将“/”放在前面。
I would try putting a tilde in. Perhaps "~/stores"?
Or, with routing, drop the "/" at the front.
如果使用“string?name”作为参数怎么办?
由于您拥有的两种方法都返回相同的内容(DTO.Stores),因此您可以使用单个方法来获取 Stores,而不是两个方法(就像您现在所做的那样)。像这样:
PS:我不确定这是否适用于 WCF,但请尝试一下。 ;-)
What if you use "string? name" as parameter?
And since both methods you have are returning the same thing (DTO.Stores) you could use a single method to get the Stores instead of two (as you are doing now). Like this:
P.S.: I am not sure if that would work well with WCF, but give it a try. ;-)