我正在尝试向客户提出请求,如果客户不存在,它应该返回某种“未找到”页面。以下哪一项是用于此类任务的最佳实践,为什么?
public ActionResult Index(int id)
{
if (customerService.GetCustomerById(id) == null)
return View("NotFound");
return View();
}
或者
public ActionResult Index(int id)
{
if (customerService.GetCustomerById(id) == null)
throw new HttpException(404, "Customer not found");
return View();
}
最佳答案
抛出一个 404。真的没有争论。这与成为 REST 门徒无关,而只是 Web 的工作方式。
您可以返回 View 和 404。帮助用户或显示搜索框或指向一些最畅销的商品通常很有帮助,但要让客户清楚地看到 NotFound 并始终在 HTTP 响应中返回 404。对此毫无疑问。
编辑:这是一个很好的指导:http://www.codinghorror.com/blog/2007/03/creating-user-friendly-404-pages.html
关于asp.net-mvc - ASP.NET MVC - HttpException 或返回 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3935298/