c# - How 2 WebAPI Routing can accept same type parameter -
i have web api has several methods. have problem routing. have 1 method returning products yearid
, method returning product product id
. here 2 routes came with:
/api/records/products?yearid=10 /api/records/products/15
these 2 methods:
[httpget] [route("getbyyearid")] public async task<product> getbyyearid(int yearid) { ..... } [httpget] [route("getbyid")] public async task<ienumerable<product>> getbyid(int productid) { ...... }
what route mapping should have can access web api these 2 routes:
/api/records/products?yearid=10 /api/records/products/15
you using attribute routing customize how want routes used.
here example of how can achieve routes wanted:
[routeprefix("api/records")] public class recordscontroller: apicontroller { // api/records/products?yearid=10 [httpget] [route("products")] public async task<product> getbyyearid(int yearid) { ..... } // api/records/products/15 [httpget] [route("products/{productid:int}")] public async task<ienumerable<product>> getbyid(int productid) { ...... } }
Comments
Post a Comment