4

I have custom errors enabled in my app. The web config has an entry as follows:

<customErrors mode="On" defaultRedirect="~/Views/Shared/Error.cshtml">
  <error statusCode="403" redirect="~/Views/Shared/UnauthorizedAccess.cshtml"/>
  <error statusCode="404" redirect="~/Views/Shared/FileNotFound.cshtml"/>
</customErrors>

I also have the HandleError attribute applied as a global action filter. My FilterConfig reads as follows:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

Further, there is the default Error.cshtml defined in the ~/Views/Shared folder.

Yet, my application displays that ugly default error page of IE.

Update

I checked that the custom error pages are showing up fine in all other browsers except IE. What's the deal here?

Further Update

Just found this article. http://perishablepress.com/important-note-for-your-custom-error-pages/

It says that IE wants custom error pages to be at least 512 bytes in size. If your custom error page is less than that size, it'll throw up its own ugly error page.

Latest Update Everything works now that I've increased the payload of my error pages. However, only the default Error.cshtml page shows up for unhandled exceptions. For the other status codes, I get an ASP.NET 404 saying that it could not find my custom error page in the location I have specified in the web.config file. I do have the custom error pages and they have the exact names I have specified in the web.config.

Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
  • Are you doing anything in the Error.cshtml that could cause an error? – ChrisBint Dec 13 '12 at 14:30
  • Make sure you have the model `@model System.Web.Mvc.HandleErrorInfo ` in the Error view. – Nate-Wilkins Dec 13 '12 at 14:32
  • @ChrisBint: Nothing. It is the one that came out of the box. I haven't touched it except for adding a model just now as per Nate's suggestion. No effect. – Water Cooler v2 Dec 14 '12 at 12:34
  • @Nate: Thanks, Nate. I just added the model definition in the Error.cshtml file. No change, though. I still get the ugly IE error page. – Water Cooler v2 Dec 14 '12 at 12:34
  • ok. I just noticed right now after testing it in Firefox that it works in Firefox and Chrome. It is only in IE that the custom error page doesn't show up. I've updated the question and added a tag. – Water Cooler v2 Dec 14 '12 at 12:36
  • I have this same problem and I'm using FireFox. So, it's not just IE. – DaveHolly Dec 29 '12 at 19:17
  • http://stackoverflow.com/questions/13905164/how-to-make-custom-error-pages-work-in-asp-net-mvc-4 –  Aug 02 '13 at 09:37
  • About the latest update . We had a similar issue but once we deployed it over the server/ or work in release mode those pages are hit for unhandled exceptions. – Nikitesh May 29 '14 at 14:21

1 Answers1

2

Instead of setting the path to your (*.cshtml) razor file, you need to provide the route through your controller and action.

Here's how I have it working as expected

<customErrors mode="Off" defaultRedirect="/home/error">
  <error statusCode="404" redirect="/home/notfound" />
</customErrors>

and in my Home Controller, I have an action called Error and another one called NotFound

[AllowAnonymous]
public class HomeController : Controller
{   
  public ActionResult Error()
  {
    var qs = HttpUtility.ParseQueryString(Request.Url.Query);
    var errorPath = qs["aspxerrorpath"];

    return View(model: errorPath);
  }

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

...
}
Guy
  • 2,883
  • 1
  • 32
  • 39