asp.net web api routing - Can I overload a Web API get call? -
i'm trying set web api app able take like
/api/product/1 1 id , /api/product/somestringidentifier
the latter can not nail get(string alias) method. (int id) , getproducts() work fine.
routes
config.routes.maphttproute( name: "defaultapi", routetemplate: "api/{controller}/{id}", defaults: new { id = routeparameter.optional } ); config.routes.maphttproute( name: "aliasselector", routetemplate: "api/{controller}/{alias}" );
controller
[acceptverbs("get")] public iproduct get(int id) { homecoming new product(id); } [acceptverbs("get")] public iproduct get(string alias) { homecoming new product(alias); } [acceptverbs("get")] [actionname("products")] public ienumerable<iproduct> getproducts() { homecoming new products().tolist(); }
assuming id going integer , alias going string, seek adding route constraints so:
config.routes.maphttproute( name: "defaultapi", routetemplate: "api/{controller}/{id}", defaults: new { id = routeparameter.optional }, constraints: new { id = @"\d*" } ); config.routes.maphttproute( name: "aliasselector", routetemplate: "api/{controller}/{alias}", constraints: new { alias= @"[a-za-z]+" } );
asp.net-web-api-routing
No comments:
Post a Comment