开发者

ASP.NET Routing - With and without a slash at the end

开发者 https://www.devze.com 2023-02-26 06:28 出处:网络
Considering the following Service Contract: [WebGet(UriTemplate = \"/stores\")] DTO.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!


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. ;-)

0

精彩评论

暂无评论...
验证码 换一张
取 消