Asp.net web api 属性路由压缩路由

Asp.net web api 属性路由压缩路由,asp.net-web-api,attributerouting,asp.net-mvc-apiexplorer,Asp.net Web Api,Attributerouting,Asp.net Mvc Apiexplorer,我正在使用WebApi的nuget包。下面是我的两个GET方法和route属性,用于list和一个特定项: [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/

我正在使用WebApi的nuget包。下面是我的两个GET方法和route属性,用于list和一个特定项:

[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)
即使“id”不是第一个GET方法的参数。如何消除结尾带有“?id={id}”的中间URI?我想我需要某种约束,但我无法从文档站点上找到它

  • 要解决此问题,可以对操作进行不同的命名。示例:GetAllProducts,GetProduct

  • 您看到的问题是预期的行为,因为ApiExplorer(HelpPage使用)访问route集合中的所有路由,并且它会检查每个路由,以查看可以从该路由访问哪些操作。现在,使用上述属性修饰路由,路由集合中的路由很可能如下所示:

  • a。“api/products/{tenantid}/{channelid}”,controller=“products”,action=“Get”等

    b。“api/products/{tenantid}/{channelid}/{id}”,controller=“products”,action=“Get”


    现在,对于路由“a”,ApiExplorer检查可以到达哪些操作,并注意到对于控制器“Products”和操作“Get”,有两个可以访问的操作,它还会尝试查看有多少参数来自路由路径本身,如果操作上有任何参数不是来自路由路径,它会假定它来自查询字符串…因此您会看到“?id={id}”。希望这有帮助。

    解决了它。我甚至没有想过要更改方法名。当切换到备用路由技术时,我应该认识到方法名不是路由约定的一部分。
    GET api/products/{tenantid}/{channelid}?status={status}&skip={skip}&take={take} 
    GET api/products/{tenantid}/{channelid}?id={id} 
    GET api/products/{tenantid}/{channelid}/{id}