属性路由限制路由

时间:2013-05-16 13:04:28

标签: asp.net-web-api attributerouting asp.net-mvc-apiexplorer

我正在使用http://attributerouting.net/ nuget包进行WebApi。以下是我的两个GET方法和路由属性,用于列表和特定项目:

[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)

但是在生成的帮助URI中,会显示三个GET选项。

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

即使“id”不是第一个GET方法的参数。如何在末尾用“?id = {id}”消除中间URI?我想我需要某种约束,但我无法从文档站点中找到它。

1 个答案:

答案 0 :(得分:2)

  1. 要解决此问题,您可以使用不同的方式命名操作。示例:GetAllProducts,GetProduct

  2. 您看到的问题是预期的行为,因为ApiExplorer(HelpPage使用)访问路径集合中的所有路径,并检查每条路线以查看可以从该路线到达的行为。现在使用上面的属性修饰路径,路径集合中的路径很可能如下所示:

  3. 一个。 “api / products / {tenantid} / {channelid}”,controller =“Products”,action =“Get”等...

    湾“api / products / {tenantid} / {channelid} / {id}”,controller =“Products”,action =“Get”......

    现在对于路线'a。',ApiExplorer会检查可以到达哪些动作,并注意到对于控制器'产品'和动作'获取',可以达到2个动作,并且它还会尝试查看许多参数来自路径路径本身,如果操作中有任何参数不是来自路径路径,则假定它来自查询字符串...因此您看到“?id = {ID}”。希望这会有所帮助。

相关问题