5

I'm building my first ASP.NET MVC website, and I'm trying to figure out how to implement a 404 page.

Should I create a controller called "404Controller?" If so, how do I then register this Controller with IIS so that it redirects 404s to that page? Also, in a situation where something is not found (in the database, for example) by some other Controller code, how would I redirect the request to my 404 page?

Maxim Zaslavsky
  • 17,787
  • 30
  • 107
  • 173

6 Answers6

14

There's no single answer for what your are trying to do, the simplest and what I like is using the HttpException class, e.g.

public ActionResult ProductDetails(int id) {

   Product p = this.repository.GetProductById(id);

   if (p == null) {
      throw new HttpException(404, "Not Found");
   }

   return View(p);
}

On web.config you can configure customError pages, e.g.

<customErrors mode="RemoteOnly" redirectMode="ResponseRewrite">
   <error statusCode="404" redirect="Views/Errors/Http404.aspx" />
</customErrors>
David Murdoch
  • 87,823
  • 39
  • 148
  • 191
Max Toro
  • 28,282
  • 11
  • 76
  • 114
7

The favorite option for me is to return a view called 404.

if (article == null)
    return View("404");

This will let you the option to have a generic 404 view in the shared folder, and a specific 404 view to the article controller.

In addition, a big plus is that there is no redirect over here.

Fitzchak Yitzchaki
  • 9,095
  • 12
  • 56
  • 96
4

MVC 3 includes a HttpNotFoundResult class and Controller has a HttpNotFound static method too.

    public ActionResult Index(int id) {
        var myThing = Loader.GetById(id);
        if (myThing == null) {
            return HttpNotFound("Thing Not Found");
        }
        return View(myThing);
    }
swilliams
  • 48,060
  • 27
  • 100
  • 130
0

Add an Application_EndRequest method to your MvcApplication per Marco's Better-Than-Unicorns MVC 404 Answer. To cause a "not found" error from your code, throw new HttpException(404, "Not Found").

Community
  • 1
  • 1
Edward Brey
  • 40,302
  • 20
  • 199
  • 253
0

In case it helps someone down the road...

Similar to Fitzchak's answer I have a custom 404 view in my shared folder. To return the correct status code I simply added this to the top of that view

@{
    ViewBag.Title = "Page Not Found";
    Response.StatusCode = 404;
}

worked great on my localhost but on my production server I had to add this line to the web config because IIS was catching the 404 status and putting up it's own 404 page.

<system.webServer>
    ...
    <httpErrors errorMode="Detailed" />
</system.webServer>
BYoung
  • 123
  • 1
  • 9
0
  1. You're correct, create a NotFoundController and map route to it, say /404 and then put the URL in your web.config file (see @R0MANARMY) answer.

  2. You can just throw new HttpException(404, "Not Found") or if you don't want to throw exceptions (which is costly) you can simply set the status code to 404 via Response.StatusCode property and then return a View("My404Template") from your action methods.

chakrit
  • 61,017
  • 25
  • 133
  • 162
  • @chakrit: Won't asp.net mvc give you a 404 anyway without a NotFoundController? Or do you mean in case you want to redirect to something other than a static html 404 page? – Roman Apr 04 '10 at 05:37