0

In Details, Edit, Delete action methods we have id parameter to retrieve the corresponding record from the database.

If no record corresponds to the id, we have 2 choices:

The executing action method returns a specific view, which is usually named as NoFound.cshtml under Views\Shared directory, to inform the user that the id is not valid.

or

The executing action method redirect the user to a specific action method, for example public ActionResult NoFound (string message), to inform the user about the issue.

My question is: When no record associated with the given id, which action should the action method do? Returning NoFound view or redirecting to NoFound action method?

Edit 1 I need reasons from technical point of view such as security and performance.

LaTeX
  • 1,411
  • 1
  • 17
  • 37

4 Answers4

9

It's all personal preference. In my opinion though, the real thing that should happen in this situation is a 404 response code should be sent back to the client. You can have custom 404 pages that give you the best of both worlds.

Matt Greer
  • 60,826
  • 17
  • 123
  • 123
  • +1 Certainly for Details a 404 Not Found makes the most sense here. For Edit and Delete, I'm wavering between a 404 or possibly a 405 "Method Not Allowed" (http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6) as you can't delete or edit a non-existent resource, but to adhere to the spec, you must send an Allows header with the allowed methods, so 404 might be more sensible. – Zhaph - Ben Duguid Mar 22 '11 at 17:24
1

Refer to Step 5 on this answer - custom 404, proper 404 response code, proper handling of the "item not found" case.

Community
  • 1
  • 1
Matt Kocaj
  • 11,278
  • 6
  • 51
  • 79
0

I would return a "Not found" view, because this way an URL with ID could be stored in favorites and will became valid when record with this ID will be available.

Alexander Prokofyev
  • 33,874
  • 33
  • 95
  • 118
0

I would recommend still returning a 404 error. The order of the parameters in the route is irrelevant, some people prefer {controller}/{action}/{id}, in some cases {controller}/{id}/{action} might make more sense. In either case if all three parameters are required for it to be considered a valid request and one is missing you should return a 404. It will also make testing your site much easier as if you return a view it will have a result code of 200 and make it much more difficult to track down broken links. 404 errors are universally recognized as a problem when doing site analysis.

Nick Albrecht
  • 16,607
  • 10
  • 66
  • 101