1

I have a basic asp.net self host app that makes use of the HttpConfiguration class and includes this route:

config.Routes.MapHttpRoute(
    name: "login",
    routeTemplate: "login",
    defaults: new { controller = "Login", action = "Get" }
);

i would like to define actions based on HTTP verbs included in the request instead of specified in the URL.

i.e.

curl http://domain/login -X GET

Should call the Get method on the LoginController class. And

curl http://domain/login -X POST -H 'Content-Length: 1' -d 'x'

Should call the Post 'Post' method in the controller class. I have been able to test that the post request works if I adjust the route configuration to

config.Routes.MapHttpRoute(
    name: "login",
    routeTemplate: "login/{action}",
    defaults: new { controller = "Login", action = "Get" }
);

and then curl to:

curl http://domain/login/post -X POST -H 'Content-Length: 1' -d 'x'

But this is specifying the post verb in the http request and URI (which I would prefer not to do).

===== EDIT

The answer by levi here: https://stackoverflow.com/a/2143991/3114742 solved my problem

Zach Smith
  • 8,458
  • 13
  • 59
  • 133
  • one way to do it is with attribute routing https://stackoverflow.com/questions/38102073/mvc-5-routing-attribute – Aaron Roberts Sep 13 '17 at 13:29
  • 1
    Possible duplicate of [How to route GET and DELETE requests for the same url to different controller methods](https://stackoverflow.com/questions/2143884/how-to-route-get-and-delete-requests-for-the-same-url-to-different-controller-me) or https://stackoverflow.com/questions/12609759/how-to-define-the-put-method-in-routing-only-limit-to-the-put-methods-in-control . – mjwills Sep 13 '17 at 13:37

0 Answers0