I need to send 401 and 403 response code while deleting the user. I am trying to use attribute based routing. Response 401 as object (application/json): {success: false} Response 403 as object (application/json): {success: false}
[Authorize]
[Route("users/{id}")]
[HttpDelete]
public ActionResult DeleteUser(Guid id)
{
try
{
using (var context = new POCContext())
{
var entity = context.Users.FirstOrDefault(e => e.UserId == id);
if (entity == null)
{
return NotFound(new { success = false });
}
else
{
context.Users.Remove(entity);
context.SaveChanges();
return NoContent();
}
}
}
catch (Exception ex)
{
return BadRequest(new { success = false });
}
}
X
as you're in a controller you can modify the
Responsedirectly in your method just doingResponse.StatusCode = 401;like the following: