asp.net mvc 4 - MVC4 Web-API - Route mapping help required -


i have controller named

products , have 3 actions in it

  • product : takes int returns model(class) object
  • category : takes string returns array of model(class) object
  • all : no parameters return array of model(class) object

what trying following mapping

product -> api / products / product / 1

category -> api / product / category / sauces

all -> api / product / all

as names of action support url structure trying general route is

config.routes.maphttproute( name: "defaultapi", routetemplate: "api/{controller}/{action}/{id}", defaults: new { id = routeparameter.optional } ); 

all else working getting following error when use http://localhost:2271/api/products/category/2 url

<error> <message> no http resource found matches request uri 'http://localhost:2271/api/products/category/2'. </message> <messagedetail> no action found on controller 'products' matches request. </messagedetail> </error> 

on other hand url api/products/category/?cat=abc & api/products/category?cat=abc working fiine...... [cat receiving parameter name]

help !!!

on other hand url api/products/category/?cat=abc & api/products/category?cat=abc working fiine

yes that's right, will, , that's rpc style of doing it. if want maintain restful way of doing can have following route configuration:

config.routes.maphttproute(     name: "productbyid",     routetemplate: "api/{controller}/{id}",     defaults: new { id = routeparameter.optional } );  config.routes.maphttproute(     name: "productbycategory",     routetemplate: "api/{controller}/category/{cat}" );  config.routes.maphttproute(     name: "defaultbyaction",     routetemplate: "api/{controller}/{action}",     defaults: new { action = "get" } );  config.routes.maphttproute(     name: "defaultapi",     routetemplate: "api/{controller}/{id}",     defaults: new { id = routeparameter.optional } ); 

and controller this:

public class productscontroller : apicontroller {     // api/products/     public ienumerable<string> get()     {         return new string[] { "value1", "value2" };                 }      // api/products/5     [httpget]     public string get(int id)     {         return "product" + id;     }      // api/products/category/fruits     [httpget]     public string category(string cat)     {         return "product " + cat;     }     } 

note: returned strings simplicity's sake assume return instance of product, , can change code so.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -