Reputation: 1459
I have a situation where I want the same arguments passed into a Web Api endpoint with different verbs. There's a C# limitation that won't allow the two identical signatures to exist. I could apply both verbs to the same signature, but then how do I check to see which verb is passed in?
Or, what is the best practice for solving this problem?
[HttpDelete]
public IHttpActionResult Logs([FromUri] string source, [FromUri] string startDate, [FromUri] string endDate)
{
return Ok();
}
[HttpGet]
public IHttpActionResult Logs([FromUri] string source, [FromUri] string startDate, [FromUri] string endDate)
{
return Ok();
}
Upvotes: 2
Views: 175
Reputation: 28737
There's two ways you can solve this:
If you use attribute routing you can apply the same route and just rename your methods.
Apply both verbs to the same method and then inside the method check which verb you're getting:
if(Request.Method == HttpMethod.Get)
{
// get it
}
else if(Request.Method == HttpMethod.Delete)
{
// delete it
}
Upvotes: 2