0

This must be simple and already answered, but I've wasted many hours on it. I can't figure how to get an error page on mistyped address. Also I'd prefer not to redirect, but to keep the URL. I've tried many combinations of CustomErrors, HttpErrors and Application_Error, but nothing works for non-existent controller - depending on HttpErrors I always get IIS 404.0 page or just an empty 404 response. Running on IIS 7.5, MVC 3.

  • possible duplicate of [How can I properly handle 404 in ASP.NET MVC?](http://stackoverflow.com/a/2577095/189950) For what it's worth, this is the approach I use. It's a *serious* PIA to set up intiially, and it really does seem overly complicated (and I'm inclined to think it is) -- **but**...it works great, is very flexible, and you can get precisely what you want. – Kirk Woll Apr 07 '12 at 03:09
  • Thx cap. Kirk ;) I'll give it a try, but IMHO it's too complicated for such a common scenario ... – user1318555 Apr 07 '12 at 03:31
  • that's fine, there are *many* solutions offered at that link. If you are looking for the simplest solution possible, that has [also been covered](http://stackoverflow.com/a/5507125/189950). – Kirk Woll Apr 07 '12 at 03:45

2 Answers2

0

I use the following route to ensure all requests not matching any other route fall there, then you can handle that case very easily:

        // this route is intended to catch 404 Not Found errors instead of bubbling them all the way up to IIS.
        routes.MapRoute(
            "PageNotFound",
            "{*catchall}",
            new { controller = "Error", action = "NotFound" }
        );

Map that last (include that statement after any other .MapRoute statements).

bevacqua
  • 47,502
  • 56
  • 171
  • 285
  • Thx Nico, I'm actually using Snooze URL, I'll check if it has a catch all. – user1318555 Apr 07 '12 at 03:29
  • This won't work for many scenarios. Many routes will match even though no action will be found! Those are technically 404s. This is why I recommended the approach in my comment. (which not coincidentally uses this solution as one component of a multi-faceted solution that handles all scenarios) – Kirk Woll Apr 07 '12 at 03:39
  • @Kirk: the point in this mapping is just to ensure any request matches a controller and action. Not to ensure any given controller or action even exists. In my example, I don't even really have a NotFound action in my error controller; but now I can handle this error myself instead of it bubbling up to IIS. – bevacqua Apr 07 '12 at 03:41
  • My point is that you cannot comprehensively handle 404 errors using merely your solution. The *duplicate* I linked to covers all facets of handling 404 errors and should be the canonical answer to this question. – Kirk Woll Apr 07 '12 at 03:42
0

I don't remember where I got the solution. But here is the code to handle the error: First, you create a ErrorController:

public class ErrorController : Controller
{
    //
    // GET: /Error/
    public ActionResult Index()
    {
        return RedirectToAction("Index", "Home");
    }

    public ActionResult Generic()
    {
        Exception ex = null;
        try
        {
            ex = (Exception)HttpContext.Application[Request.UserHostAddress.ToString()];
        }
        catch { }

        return View();
    }

    public ActionResult Error404()
    {            
        return View();
    }
}

Second, open Global file and add the following code:

protected void Application_Error(object sender, EventArgs e)
{
     Exception ex = Server.GetLastError();
     Application[HttpContext.Current.Request.UserHostAddress.ToString()] = ex;
}

Third, change customerror in your webconfig:

<customErrors mode="Off" defaultRedirect="/Error/Generic">
  <error statusCode="404" redirect="/Error/Error404"/>
</customErrors>

More: I created one more error layout. It makes things even more clear. :)

Hope this helps you.

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480