0

at first I had ordinary GET/POST/PUT/DELETE requests to my server, and this config.route of my web api was enough:

            config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
            );

then, I needed a custom action (for example, instead of api/User/212 - GET, I want to api/GetAllUsers - GET)

so I changed the config to that:

config.Routes.MapHttpRoute(
            name: "DefaultApiWithAction",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { action = "get" ,id = RouteParameter.Optional }
            );
        config.Routes.MapHttpRoute(
            name: "DefaultApiGet",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { action = "get", id = RouteParameter.Optional }
            );
        config.Routes.MapHttpRoute(
            name: "DefaultApiPost",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { action = "post", id = RouteParameter.Optional }
            );
        config.Routes.MapHttpRoute(
            name: "DefaultApiPut",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { action = "put", id = RouteParameter.Optional }
            );
        config.Routes.MapHttpRoute(
            name: "DefaultApiDelete",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { action = "delete", id = RouteParameter.Optional }
            );

the problem now, is that I can't reach the classic GET ('DefaultApiGet).

EDIT

In another words, if I have a user that I want to get his one post, or all posts then I want to be able to do that (user id = 12345): 'api/UserPost/12345' - get last post of user 12345 and 'api/UserPost/GetAll/12345' - get all posts of user 12345

HS1
  • 608
  • 2
  • 10
  • 28
  • Can you try removing `id = RouteParameter.Optional` in your "DefaultApiWithAction" route? – Milen Aug 28 '14 at 08:46
  • I tried, but now the POST is not working. I get: 405 - method not allowed – HS1 Aug 28 '14 at 08:54
  • Is this of any help: http://stackoverflow.com/questions/23293782/mvc-web-api-405-method-not-allowed – Milen Aug 28 '14 at 09:02

1 Answers1

0

The problem and the solution are well explained here

HS1
  • 608
  • 2
  • 10
  • 28