asp.net mvc how to return page not found from an action method Edit
In this post, we will look at how to handle asp .net 404 error in asp .net MVC application. Generally, a 404 error has to be returned if the URL does not match any route in the routing table or when a suitable controller is not found or controller action is not found. In my case, I wanted to return page not found exception when the parameter id is not found in the database. So I had to return a 404 error from my action method.
Solution: ASP.NET MVC 3
HttpNotFoundResult action result as introducted in asp .net MVC 3.0 which could be used to throw an exception from your code which can be done using HttpNotFound().
public ActionResult MyActionMethod() { // Your code here return HttpNotFound(); }
Solution: Prior to MVC 3
Prior to asp.net MVC 3 you had to raise an HttpException like in the code below.
throw new HttpException(404, "HTTP/1.1 404 Not Found");