locked
Multiple POST type action in web api is possible RRS feed

  • Question

  • User264732274 posted

    i just like to know can a controller can have multiple action with HttpPost verbs ?

    if no then let me know the reason.

    thanks

    Monday, November 9, 2015 9:39 AM

Answers

  • User281315223 posted

    While my example would imply that this is MVC as it uses ActionResult, this was just for example purposes. The same holds true for Web API as the only thing that differs is the return type being used (i.e. not an ActionResult) :

    [HttpPost]
    public IEnumerable<string> A() { ... }
    
    [HttpPost]
    public void B() { ... }
    
    [HttpPost]
    public bool C() { ... }

    sudip_inn

    does ActionResult has any use in web api?

    Not really as Web API isn't really going to be returning any "actions" or rendering any Views. It does more commonly used the IHttpActionResult as a base return type, but you'll usually see Web APIs return actual data.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 9, 2015 1:05 PM

All replies

  • User281315223 posted

    sudip_inn

    i just like to know can a controller can have multiple action with HttpPost verbs ?

    if no then let me know the reason.

    thanks

    Yes, you can have multiple actions that accept POST requests within a single controller : 

    [HttpPost]
    public ActionResult A() { ... }
    
    [HttpPost]
    public ActionResult B() { ... }
    
    [HttpPost]
    public ActionResult C() { ... }
    
    
    

    Monday, November 9, 2015 9:55 AM
  • User264732274 posted

    i asked this question in the context of Web API not MVC. from your code it seems that you are talking about MVC because all the method return ActionResult which is MVC related. does ActionResult has any use in web api?

    thanks

    Monday, November 9, 2015 1:00 PM
  • User281315223 posted

    While my example would imply that this is MVC as it uses ActionResult, this was just for example purposes. The same holds true for Web API as the only thing that differs is the return type being used (i.e. not an ActionResult) :

    [HttpPost]
    public IEnumerable<string> A() { ... }
    
    [HttpPost]
    public void B() { ... }
    
    [HttpPost]
    public bool C() { ... }

    sudip_inn

    does ActionResult has any use in web api?

    Not really as Web API isn't really going to be returning any "actions" or rendering any Views. It does more commonly used the IHttpActionResult as a base return type, but you'll usually see Web APIs return actual data.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 9, 2015 1:05 PM