【发布时间】:2017-08-08 14:35:37
【问题描述】:
我一直想知道在以下情况下最好的错误响应代码是什么:
public IActionResult Index(Guid id)
{
var entity = _dbContext.Entities.Find(id);
if (entity == null)
{
return NotFound(); //??
}
return View(entity)
}
404 - Not Found 似乎最合适,但是,从调试的角度来看,不存在的 ID 和错误的控制器/动作名称是非常不同的错误。
无论如何,我决定返回带有更多解释性消息的自定义错误页面,这样我就可以区分 404 和 404。
Controller的Action如何返回自定义404页面,其他情况下返回默认404?
我想避免返回 HttpResponseMessage,因为我需要更改 Action 的返回类型。
PS:在 cmets 中,您可以投票支持 404 和其他您将在这种特殊情况下使用的响应代码。
【问题讨论】:
-
你试过 return RedirectToAction("FourZeroFourpage"); ?
-
重定向到操作返回 302,这不是我想要的
-
您已经使用了最好的方法。
if (entity == null) { return NotFound(); //?? }
标签: c# asp.net-core asp.net-core-mvc