0

I have this controller

[ApiController]
    [Route("api/[controller]")]
    public class FamiliesController : ControllerBase
    {
        readonly FamilyFinanceContext db;
        public FamiliesController(FamilyFinanceContext context)
        {
            db = context;
        }


        [HttpDelete("deletefamily")]
        public async Task<ActionResult<Family>> DeleteFamilly(int id)
        {
            Family user = db.Families.FirstOrDefault(x => x.Id == id);
            if (user == null)
            {
                return NotFound();
            }
            db.Families.Remove(user);
            await db.SaveChangesAsync();
            return Ok(user);
        }
    }

after call https://localhost:44373/api/families/deletefamily?id=2 i have this error - HTTP ERROR 405 In theory this GET parameters must work. What i done not correctly?

3
  • 3
    You're asking for a DELETE ([HttpDelete("deletefamily")]) not a GET. Do you need to support both? Commented Feb 7, 2020 at 22:09
  • 1
    how do you calling this API? I think the problem is on your client-side Commented Feb 7, 2020 at 22:10
  • if you are using a web browser such as Chrome to do the call, that will do a GET and your controller is expecting a DELETE Commented Feb 8, 2020 at 18:32

1 Answer 1

0

As ESG stated, your trying to do a DELETE, so you need to use the DELETE verb, not the GET verb. You're getting a 405 Method Not Allowed for this reason (you cannot use GET on a DELETE action). You should use a tool like PostMan (https://www.postman.com/) to create your DELETE requests, since you can't really do it easily just in a browser.

To fall more in line with REST convention, you should consider changing your DELETE method slightly:

    [HttpDelete("{id}")]
    public async Task<ActionResult> DeleteFamily([FromRoute] int id)
    {
        Family user = db.Families.FirstOrDefault(x => x.Id == id);
        if (user == null)
        {
            return NotFound();
        }
        db.Families.Remove(user);
        await db.SaveChangesAsync();
        return NoContent();
    }

You would then call this as DELETE https://localhost:44373/api/families/2

By using the [HttpDelete("{id}"] attribute, your moving the id from the query string to the URI, which is more in line with REST convention (the URI represents an object). Query string parameters are more typically used for optional capabilities, such as filtering, sorting, etc and not the endpoint representing the object itself.

Typically DELETE actions do not return content, but that is up to you. If you really want to return the user object, then stick with Ok(user), but NoContent is more typical of the DELETE verb.

Sign up to request clarification or add additional context in comments.

2 Comments

You should also use decorators for the parameters, in your case [FromRoute] int id, or if he leaves it as a query parameter [FromQuery] int id. Also, specifying the type of the parameter as in {id:long} is only necessary if you have routes that are otherwise the same except for the type, which, IMO, you really shouldn't be doing anyway.
@Bodacious, those are good points. I never really meant to have the type in there ("long") but I cut and pasted from some other code. I've updated the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.