ASP.NET MVC HandleError not working (customErrors is set to "On")
Asked Answered
C

7

14

I believe I have set up our MVC app to use [HandleError] properly. This is my controller:

[HandleError]
public class SupportController : BaseController {

    public ActionResult Toss() {
        throw new Exception("uh oh");
    }
    // snip
}

And I have set my customErrors tag in web.config to "On":

<customErrors mode="On"></customErrors>

However, I am still getting the Yellow Screen of Death on Exceptions. Setting a breakpoint in my Toss() action shows that HttpContext.IsCustomErrorEnabled is set to true.

We are not doing anything with the View Engine, and the BaseController doesn't mess with anything either (and other Controllers that do not extend it have the same issue).

I am developing on Windows XP, and have the same issue when the app is deployed to a server 2003 box (IIS 6).

I do not think there is an Exception on the error.aspx page:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>

<asp:Content ID="errorContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        Sorry, an error occurred while processing your request.
    </h2>
</asp:Content>

One thing that may be different is that this app was created back when MVC Beta was the latest version, and was upgraded to the RC and then RTM as they were released. Might there have been some kooky setting that is left over from there?

I can get this working on other apps, so I'm a little flummoxed.

Columbarium answered 15/6, 2009 at 22:55 Comment(2)
Is your error.aspx page in ~\Views\Support or ~\Views\Shared?Sami
~\Views\Shared - even tried [HandleError(View = "~/Views/Shared/Error.aspx")] (didn't work)Columbarium
A
20

This tends to happen if there is a problem processing the error page.. if you debug the app, right after the initial exception you'll problem hit another (exception from the error page).. I had this happening and the reason for me was because I had a strongly typed master page, the error page was using this master page, and because the masterpage shares the same model as the actual page, the master page was getting a HandlerErrorInfo model, instead of the typed model I expected..

Personally I think this is a poor design in the asp.net mvc (along with the rest of it), but you can get around this easy enough by not using the same master page (you could even do masterpage inheritance where you have an inherited strongly typed master page that purely inherits the layer from the untyped one..

Otherwise this is some sort of exception happening in the error view.. (most likely).

Advantage answered 17/6, 2009 at 14:22 Comment(4)
You weren't 100% right, but you put me on the right track. The Master Page does not have a model, but it does do some stuff that was presumably throwing a null reference exception. Using a non master paged view works. Thanks!Columbarium
Happy to help, plenty of headache points in asp.net mvc, I seem to keep finding them and figured I should save somebody some 'head to keyboard impact' actions. :)Advantage
Been a while since this post but it was helpful due for me as wellFezzan
Exactly the issue I was having, stackoverflow to the rescue once again! Thank you.Befool
P
3

If you are using IE9 there is another issue. It seems IE9 hide the error page if it is less than 1Kb, as said in:

asp-net-mvc-3-handleerror-global-filter-always-shows-iis-status-500-page

Be sure content body is more than 1Kb length. Try adding this at the end of the error page:

@(new String(' ', 1000))

Plat answered 2/3, 2012 at 21:48 Comment(1)
Not just in IE9. I have IE8 and experience the same behaviour.Monosyllabic
V
2

Another reason for this problem may be ,

In Template MVC Application (generated by VS2008 / VS2008 Express) , Error.aspx (generated by VS) uses Master Page.

If Master Page access any ViewData it will throw null reference Exception , then the error.aspx won't be shown.

Use this Simple code as your Error.aspx , it will solve the problem, (along with CustomErrors=On )

<%@ Page Language="C#"  Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>" %>
<%= Model.Exception.Message %>
Valentinavalentine answered 27/7, 2009 at 14:20 Comment(0)
M
1

I had a same problem, and after investigating, I found that I was updating the web.Debug.config file under web.config.

Then I created the the CustomErrors tag in the web.config file and it was working for me.

Megaphone answered 9/7, 2010 at 16:31 Comment(0)
M
1

I kept running into a similar problem. I was able to get a desirable result with this "hack".

Since all my controllers extend a BaseController, this works everywhere.

public class BaseController : Controller
{
    protected override void OnException(ExceptionContext filterContext)
    {

        Response.Write("<pre>" + filterContext.Exception.StackTrace + "</pre>");

        base.OnException(filterContext);

    }

}
Manganate answered 20/1, 2012 at 2:41 Comment(0)
R
1

I ran into a similar issue recently. It turned out that the method in the controller that throws the exception (HomeController) was being called by a method in a different controller (DispatchController).

We had to place the [HandleError] property on the calling controller to get it to work.

[HandleError(ExceptionType = typeof(SessionExpireException), View = "ErrorSessionExpired")]
public class DispatchController : Controller
{

We also placed the custom error view in the folder structure for the calling controller.
View folder showing custom error view in the Dispatch controller folder

Rosalba answered 8/1, 2015 at 18:13 Comment(0)
I
0

After much hair-pulling, I found that my problem falls into the general category of "throwing exception from code only to throw another error when rendering the error view." I had written a strongly-typed error page earlier expecting a System.Exception, then when I switched to using the [HandleError] attribute, the filter was passing the error page a System.HandleErrorInfo. The second exception happened at this line (in Default.aspx.cs):

httpHandler.ProcessRequest(HttpContext.Current);

Confusing.

Illustrative answered 18/11, 2009 at 20:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.