Routing in Web Api for Get() method's overload

Topics: ASP.NET Web API
Apr 12, 2014 at 10:17 PM
I have following code in my Asp.Net web api controller
 class MyController:ApiController
 {
    public returntype1 Get(string somevalue)
    {
    }

     public returntype2 Get(string somevalue, int id)
     {
     }
  }
And the routing method that I have is
   class RouteConfig
   {

     public static void RegisterRoutes(HttpConfiguration config) 
     {
    var routes = config.Routes;  
    routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{somevalue}/{id}",
    defaults: new {action = "Get", somevalue = RouetParameter.optional,id=RouteParameter.Optional }
      );
  }
}

And I have url for the first Get(string somevalue) method like /api/My/get?somevalue=hello

And for second Get(string somevalue, int id) overload like /api/My/get?somevalue=hello&id=1234

but I want to make some changes in routing so that for 1st Get method I can have url like /api/My/hello and for second overload Get method I can have url like /api/My/hello/1234 Kindly help I'm new to web api and routing. Thanks
Apr 13, 2014 at 6:58 PM
For the URLs you show without the "get" in them, you would want to remove {action} from the route template. Query string parameters always match the method signature by name anyway, and the method name would match against the HTTP verb with the default resource-based routing.