Ciccio Ciccio - 1 month ago 10
reST (reStructuredText) Question

How to have 2 method with same sign to delete resource of REST service

I have a REST service and I should have two delete methods. Each method expects just one ID.

The first method delete A LOT OF elements:

[HttpDelete]
public IHttpActionResult DeleteNPs(int countryId)
{
...
return Ok();
}


The second method delete just ONE element:

[HttpDelete]
public IHttpActionResult DeleteNP(int npId)
{
...
return Ok();
}


As you can see the sign is the same, but the effect is totally different. I had thought to create the second method with two parameters, like this:

[HttpDelete]
public IHttpActionResult DeleteNPs(int countryId, int npId)
{
...
return Ok();
}


But this is a really bad work-around... and neither I do have the country id in the second case. How do I have to manage the following situation with the REST paradigm?

Thank you

Answer

IMHO if you really rely on some REST standards, you have something wrong with your design.

Normally you'd have following methods:

[HttpDelete]
public IHttpActionResult DeleteNPs()
{
    ...
    return Ok();
}

[HttpDelete]
public IHttpActionResult DeleteNP(int npId)
{
    ...
    return Ok();
}

If you would like to introduce a parameter, you should introduce another method:

[HttpDelete]
public IHttpActionResult DeleteNPsByCountryId(int countryId)
{
    ...
    return Ok();
}

and attach e.g. following routes:

  • DELETE /api/np
  • DELETE /api/np/1
  • DELETE /api/np/country/1

in other case you're making your API ambiguous.

Comments