ASP。NET MVC - HttpException还是返回视图?

时间:2022-11-24 11:21:53

I'm trying to make a request for a customer and if the customer doesn't exist it should return some kind of "Not found" page. Which of the below would be the best practice to use for such a task, and why?

我正在尝试向客户提出请求,如果客户不存在,它应该返回某种“未找到”页面。下面哪一种方法是用于此类任务的最佳实践,为什么?

public ActionResult Index(int id)
{
    if (customerService.GetCustomerById(id) == null)
        return View("NotFound");

    return View();
}

or

public ActionResult Index(int id)
{
    if (customerService.GetCustomerById(id) == null)
        throw new HttpException(404, "Customer not found");

    return View();
}

2 个解决方案

#1


6  

Throw a 404. There's really no argument. It's not about being a REST disciple, it's just how the web works.

把一个404。没有参数。这不是要做一个休息的门徒,这只是网络的运作方式。

You can return a view and a 404. It's often helpful to help the user or present a search box or point to some top selling items, but make the NotFound clear to the customer and always return a 404 in the HTTP response. No question about that.

您可以返回一个视图和一个404。帮助用户或提供一个搜索框或指向一些*的销售项目通常是有帮助的,但是要让客户明白并始终在HTTP响应中返回404。这是毫无疑问的。

Edit: This is good guidance: http://www.codinghorror.com/blog/2007/03/creating-user-friendly-404-pages.html

编辑:这是一个很好的指南:http://www.codinghorror.com/blog/2007/03/create -user- friendly404 -pages.html

#2


2  

This is a good question (+1) as there are some differing opinions out there about when to use HTTP exception codes and when not.

这是一个很好的问题(+1),因为对于什么时候使用HTTP异常代码,什么时候不使用HTTP异常代码,人们有不同的看法。

REST disciples will likely tell you to go the HTTP Exception route because they believe that a URI identifies a conceptual resource (i.e.: the actual object/thing to which you are referring - a Customer in this case), and if that resource doesn't exist then you should get a 404 error back.

REST门徒可能会告诉您使用HTTP异常路由,因为他们认为URI标识了概念资源(即::您所指的实际对象/对象(在本例中为客户),如果该资源不存在,则应返回404错误。

However, some will disagree and say that you should only pass back a 404 error if the physical resource, e.g. a file, doesn't exist.

但是,有些人会不同意,并说,如果物理资源(例如文件)不存在,那么应该只返回404错误。

I tend to fall into the second camp and would recommend that you return 200 OK with a custom view stating that the customer specified by the ID could not be found.

我倾向于进入第二个阵营,并建议您返回200 OK,并使用自定义视图,声明没有找到该ID指定的客户。

#1


6  

Throw a 404. There's really no argument. It's not about being a REST disciple, it's just how the web works.

把一个404。没有参数。这不是要做一个休息的门徒,这只是网络的运作方式。

You can return a view and a 404. It's often helpful to help the user or present a search box or point to some top selling items, but make the NotFound clear to the customer and always return a 404 in the HTTP response. No question about that.

您可以返回一个视图和一个404。帮助用户或提供一个搜索框或指向一些*的销售项目通常是有帮助的,但是要让客户明白并始终在HTTP响应中返回404。这是毫无疑问的。

Edit: This is good guidance: http://www.codinghorror.com/blog/2007/03/creating-user-friendly-404-pages.html

编辑:这是一个很好的指南:http://www.codinghorror.com/blog/2007/03/create -user- friendly404 -pages.html

#2


2  

This is a good question (+1) as there are some differing opinions out there about when to use HTTP exception codes and when not.

这是一个很好的问题(+1),因为对于什么时候使用HTTP异常代码,什么时候不使用HTTP异常代码,人们有不同的看法。

REST disciples will likely tell you to go the HTTP Exception route because they believe that a URI identifies a conceptual resource (i.e.: the actual object/thing to which you are referring - a Customer in this case), and if that resource doesn't exist then you should get a 404 error back.

REST门徒可能会告诉您使用HTTP异常路由,因为他们认为URI标识了概念资源(即::您所指的实际对象/对象(在本例中为客户),如果该资源不存在,则应返回404错误。

However, some will disagree and say that you should only pass back a 404 error if the physical resource, e.g. a file, doesn't exist.

但是,有些人会不同意,并说,如果物理资源(例如文件)不存在,那么应该只返回404错误。

I tend to fall into the second camp and would recommend that you return 200 OK with a custom view stating that the customer specified by the ID could not be found.

我倾向于进入第二个阵营,并建议您返回200 OK,并使用自定义视图,声明没有找到该ID指定的客户。