19

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();
}
3
  • What would the Exception mean to the end user? Also, why are you emitting links to non-existing Customer IDs? :) Commented Oct 14, 2010 at 16:20
  • It should just tell the end user that the user dosent exist. Answer to your second question is that if someone delete a customer, and another havent updated his/her browser yet then the link is still listed and when clicked it would throw an unexpected exception. Commented Oct 14, 2010 at 16:22
  • Just to back ebb up, what if a human enters a bad id by hand? 404. Commented Jun 27, 2012 at 11:25

2 Answers 2

6

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

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.

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

Sign up to request clarification or add additional context in comments.

Comments

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.

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.

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.

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.

10 Comments

A custom view that takes a parameter for error message, and response code as 200 would be the way forward?
That would be my recommendation, yes. Like I said, it's just an opinion and not necessarily a codified best practice, so really you can do whatever you want.
Wouldn't it be a bit safer for SEO reasons to throw the 404?
@David - I can't comment on the SEO implications of choosing one or the other. Like I mentioned in my post, it's a matter of opinion/preference ultimately.
You can display a friendly page and throw the 404 at the same time that way both people and computers can understand its not found...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.