【发布时间】:2020-08-06 18:48:09
【问题描述】:
我需要在删除用户时发送 401 和 403 响应代码。我正在尝试使用基于属性的路由。响应 401 作为对象(应用程序/json): {成功:假} 响应 403 作为对象(应用程序/json): {成功:假}
[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
【问题讨论】:
-
还有什么问题?你的代码的结果是什么?
-
我收到 500 响应代码。我正在使用提琴手进行测试。删除方法,这是我的请求。localhost:44384/users/62C2428D-9A7B-40F2-94F1-2200C6C064C6.. 我做得对吗?
-
500 表示抛出异常。您将需要调试代码以查看它的来源。如果您使用错误的详细信息更新您的问题,也许我们可以为您提供进一步的帮助。
标签: asp.net-core asp.net-web-api asp.net-core-2.1 http-status-codes entity-framework-core-2.1