0

Why does the default Web Api 2 http routing not contains {action}:

From the microsoft sample:

// Configure Web API for self-host. 
HttpConfiguration config = new HttpConfiguration(); 
config.Routes.MapHttpRoute( 
    name: "DefaultApi", 
    routeTemplate: "api/{controller}/{id}", 
    defaults: new { id = RouteParameter.Optional } 
); 

Why does it not look like this:

// Configure Web API for self-host. 
HttpConfiguration config = new HttpConfiguration(); 
config.Routes.MapHttpRoute( 
    name: "DefaultApi", 
    routeTemplate: "api/{controller}/{action}/{id}", 
    defaults: new { id = RouteParameter.Optional } 
); 

Why is the action part not required for correct routing?

The code is rom http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api

Thanks a lot!

BendEg
  • 20,098
  • 17
  • 57
  • 131
  • Because in that mode the methods are matched by Http Verbs (Get , POST, PUT) and by parameters. You can add `{action}` and then that would match by the method name. – Zein Makki Sep 13 '16 at 08:35

1 Answers1

1

The actions in the example are matched implicitly. If you look, they are all different HTTP verbs.

Mark Walsh
  • 3,241
  • 1
  • 24
  • 46
  • Ok, so adding `{action}` would not cause any problems? Thanks a lot! – BendEg Sep 13 '16 at 08:26
  • 1
    If I were you, I would use [Attribute Routing](http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2) instead. It's more explicit. I have never had to change the default route. – Mark Walsh Sep 13 '16 at 08:29
  • Thank you for that hint! – BendEg Sep 13 '16 at 08:30
  • If you want a specific answer to your question, this question is very similar http://stackoverflow.com/questions/13596391/web-api-routing-api-controller-action-id-dysfunctions-api-controller – Mark Walsh Sep 13 '16 at 08:32