ASP.NET Routing - With and without a slash at the end
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!
I would try putting a tilde in. Perhaps "~/stores"?
Or, with routing, drop the "/" at the front.
What if you use "string? name" as parameter?
[WebGet(UriTemplate = "/stores/{name}")]
DTO.Stores GetStores(string? name);
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:
[WebGet(UriTemplate = "/stores/{name}")]
DTO.Stores GetStores(string? name)
{
if(string.IsNullOrEmpty(name))
{
//get specific store
}
else
{
//get all stores
}
}
P.S.: I am not sure if that would work well with WCF, but give it a try. ;-)
精彩评论