Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core MVC 1.0 multiple post method in one Controller

I can do this by using this code :

    [HttpPost("SampleRoute1")]
    public JsonResult Post([FromBody]SampleModel1 value)
    {
        .....Functionone.....
        return Json("");
    }

    [HttpPost("SampleRoute2")]
    public JsonResult Post([FromBody]SampleModel2 value)
    {
        .....Functiontwo.....
        return Json("");
    }

but i cant do this :

    [HttpPost("SampleRoute1")]
    public JsonResult Post([FromBody]SampleModel1 value)
    {
        .....Functionone.....
        return Json("");
    }

    [HttpPost("SampleRoute2")]
    public JsonResult Post([FromBody]SampleModel1 value)
    {
        .....Functiontwo.....
        return Json("");
    }

it gives error "Type 'Controller1' already defines a member called 'Post' with the same parameter types"

so is there any way that i can make two Post in one controller with same paramter but with different route?

like this :

Posting(SampleModel1) => "Controller1\SampleRoute1" => Doing Function1

Posting(SampleModel1) => "Controller1\SampleRoute2" => Doing Function2

like image 352
Ahad Porkar Avatar asked Feb 27 '26 04:02

Ahad Porkar


1 Answers

Yes, you can do that. Problem is that you're trying to have two methods in a class that have same name & parameters and that's not possible. You should change name of your methods to something different.

Note that the action name & Post request type are already specified in the HttpPost attribute so you don't have to rely on the method name.

[HttpPost("SampleRoute1")]
public JsonResult Aaa([FromBody]SampleModel1 value)
{
    .....Functionone.....
    return Json("");
}

[HttpPost("SampleRoute2")]
public JsonResult Bbb([FromBody]SampleModel1 value)
{
    .....Functiontwo.....
    return Json("");
}
like image 180
Martin Vich Avatar answered Mar 02 '26 01:03

Martin Vich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!