質問

I'm using the http://attributerouting.net/ nuget package for WebApi. Here are my two GET methods and route attributes, for list and a specific item:

[GET("api/products/{tenantid}/{channelid}?{skip=0}&{take=20}&{status=1}")]
public IEnumerable<Product> Get(short tenantId, byte channelId, int status, int skip, int take)

[GET("api/products/{tenantid}/{channelid}/{id}")]
public Story Get(short tenantId, byte channelId, long id)

But in the generated help URIs, three GET options are shown.

GET api/products/{tenantid}/{channelid}?status={status}&skip={skip}&take={take} 
GET api/products/{tenantid}/{channelid}?id={id} 
GET api/products/{tenantid}/{channelid}/{id}

even though "id" isn't a parameter to the first GET method. How do I eliminate the middle URI with "?id={id}" at the end? I imagine I need some sort of constraint, but I can't figure it out from the documentation site.

役に立ちましたか?

解決

  1. To fix the issue, you can name the actions differently. Example: GetAllProducts, GetProduct

  2. The issue you are seeing is an expected behavior because ApiExplorer(which HelpPage uses) visits all routes within the route collection and for each route it checks to see which actions can be reached from that route. Now with the above attribute decorated routes, the routes in the route collection most probably would like below:

a. "api/products/{tenantid}/{channelid}", controller="Products", action = "Get" etc...

b. "api/products/{tenantid}/{channelid}/{id}", controller="Products", action = "Get"...

Now for the route 'a.', ApiExplorer checks which actions can be reached and it notices that for the controller 'Products' and action 'Get', there are 2 actions that can be reached and also it tries to see how many parameters are coming from the route path itself and if there are any parameters on the action which aren't coming from the route path, it assumes it to be coming from the query string...hence you are seeing the "?id={id}". Hope this helps.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top