Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for attempting to edit an item that doesn't exist?

I currently have this controller function:

public ViewResult Edit(int id)
{
    //get user from id
    var user = _adminRepository.GetUser(id);

    return View(user);
}

This currently gives me an error on my view page if I attempt to edit an item with an id of 100, when there is no user with an id of 100 in the database.

What's the best practice for handling this? Send them to a Create page, or show a friendly error message? Should that redirect functionality be within the controller function?

like image 490
Steven Avatar asked Feb 02 '11 00:02

Steven


1 Answers

IMO it should raise a 404 error. After all the user is requesting a resource that does not exist, much like a regular web page.

like image 148
Adrian Grigore Avatar answered Nov 14 '22 21:11

Adrian Grigore