0

I have two Post method one is taking single object and one is taking list of object, how to do this overriding in .net core?

// taking list of object
[HttpPost("matchdob")]
public MatchDOBResponse MatchDOB(List<DOBMatchRequest> requestModel)
{
  //
}
// taking single object
[HttpPost("matchdob")]
public MatchDOBResponse MatchDOB(DOBMatchRequest requestModel)
{
  //
}

its not working, how to solve this issue?

SP Tutorials
  • 15
  • 2
  • 7
  • Welcome to the stack overflow!! First of all whatever example you provide its is know as **`Method Overloading`**. – Kiran Joshi Feb 02 '22 at 04:15
  • you could simply have a list itself and send one `DOBMatchRequest` in it instead a over ride, I don't think that would be possible – Ravikumar B Feb 02 '22 at 04:16
  • yes if it would be a array list then I know it, but I am asking about, is it possible by overriding? – SP Tutorials Feb 02 '22 at 04:18
  • No with overload, because you can't expose 2 APIs with same URL. But you can try this https://stackoverflow.com/questions/48522097/asp-net-core-web-api-same-url-with-different-query-parameter – Ravikumar B Feb 02 '22 at 04:19
  • https://stackoverflow.com/questions/436866/can-you-overload-controller-methods-in-asp-net-mvc have look into it – Kiran Joshi Feb 02 '22 at 04:20
  • 1
    You cannot do action overloads. The way routing works in ASP.NET Core is different than how it did in ASP.NET Web Api. You need to choose different names for your handlers. – Rahul Sharma Feb 02 '22 at 04:22

2 Answers2

1

You can't set the multiple action with the same template name. Each action should have a unique route name.

Try use different name, like this:

// taking list of object
[HttpPost("matchdmultipleob")]
public MatchDOBResponse MatchDOB(List<DOBMatchRequest> requestModel)
{
  //
}
// taking single object
[HttpPost("matchdsingleob")]
public MatchDOBResponse MatchDOB(DOBMatchRequest requestModel)
{
  //
}

Or you can use one action method and receive a list of object, then check the count:

[HttpPost("matchdob")]
public MatchDOBResponse MatchDOB(List<DOBMatchRequest> requestModel)
{
    if (requestModel.Count == 1)
    {
        //upload single object
        //do something
    }
    else
    {
        //upload list of object
    }

    //
    return new MatchDOBResponse() { Status = "200", Message = "Success" };
}
Zhi Lv
  • 18,845
  • 1
  • 19
  • 30
0

Actually it is possible to implement what you are asking. It is necessary to use the route constraints.

For example, the following two action method will be called with the same action name Method depend on the data parameter type: the first when data is int, and the second when the data parameter is bool.

[HttpPost, Route("{data:int}")]        
public IActionResult Method(int data)
{
    return RedirectToAction("Method1", data);
}

[HttpPost, Route("{data:bool}")]        
public IActionResult Method(bool data)
{
    return RedirectToAction("Method2", data);
}

In your case the Custom route constraints should be implemented to use this feature: one class to support DOBMatchRequest type, and the second class for List<DOBMatchRequest> type.

For more information see:

Route constraints

Custom route constraints

Note: Yes... this is more likely overloading, not overriding.

Jackdaw
  • 7,626
  • 5
  • 15
  • 33