Vic F
Vic F

Reputation: 1459

Web Api Same Signature Different Verbs

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

Answers (1)

Kenneth
Kenneth

Reputation: 28737

There's two ways you can solve this:

  1. Attribute Routing

If you use attribute routing you can apply the same route and just rename your methods.

  1. Checking the verb inside the handler

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

Related Questions