c# - WebAPI - routes -


i new webapi , don't understand how routing in webapi v2 works.

i've created simple test controller:

public class testcontroller : apicontroller {     public list<string> getall()     {         return new list<string>();     }      public string get(int id)     {         return string.empty;     }      public string getsmthbyparam1(int param)     {         return string.empty;     }      public string getsmthbyparam2(int param)     {         return string.empty;     }      public list<string> getallbyparam(int param)     {         return new list<string>();     } } 

i reach each method by:

/api/test/getall /api/test/get/3 /api/test/getsmthbyparam1/1 /api/test/getsmthbyparam2/1 /api/test/getallbyparam/1 

and don't know how achieve it. changed routes in webapiconfig.cs to:

config.routes.maphttproute("defaultapiwithaction", "api/{controller}/{action}/{id}", new { id = routeparameter.optional }, new { id = @"\d+" }); 

below code should work you,

[routeprefix("test")] public class testcontroller : apicontroller { [route("getall")] public list<string> getall() { return new list<string>(); } [route("get")] public string get(int id) { return string.empty; } [route("getsmthbyparam1/{param}")] public string getsmthbyparam1(int param) { return string.empty; } [route("getsmthbyparam2/{param}")] public string getsmthbyparam2(int param) { return string.empty; } [route("getsmthbyparam/{param}")] public list<string> getallbyparam(int param) { return new list<string>(); } }

your config should config.maphttpattributeroutes();


Comments