attribute does not seem to act at all
Asked Answered
C

6

11

I am having problems using the [HandleError] attribute on my Controller Actions - it doesn't seem to work at all (i.e. it doesn't matter if the filter is there or not - I get the same results...). When an Exception is thrown, I get the standard red-toned Server Error in '/' Application error page instead of my custom view.

I have found a couple of other threads on the subject here on SO, and in most cases it seems that setting the customErrors option to On in web.config solved the problem. It has not for me, so I need to find a different solution.

My controller action:

[HandleError]
public ActionResult Index()
{
    throw new Exception("oops...");
    return View();
}

In my web.config file

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

I have made sure that the Error.aspx file is in the Shared directory, too. What am I missing?

I am running ASP.NET MVC RC Refresh.

Cytaster answered 8/2, 2009 at 0:55 Comment(0)
L
14

Two useful things to know:

By default, HandleError does nothing when running under the development server. The intention is to show developers more useful information:

public virtual void OnException(ExceptionContext filterContext) {
    if (filterContext == null) {
        throw new ArgumentNullException("filterContext");
    }

    // If custom errors are disabled, we need to let the normal ASP.NET
    // exception handler execute so that the user can see useful
    // debugging information.
    if (filterContext.ExceptionHandled
        || ! filterContext.HttpContext.IsCustomErrorEnabled) {
        return;
    }

Note that this case is precisely what customError is supposed to control. If setting customError="On" does not change this behavior:

  1. Check your syntax.
  2. Make sure you're editing the Web.config in the project root, not the one in Views.
  3. Make sure no code sets HttpContext.IsCustomErrorEnabled.
  4. If all else fails, try turning debug off in Web.config

Second, there certain types of errors which HandleError will never handle, notably ASP.NET compilation errors. You don't say which error you're encountering.

Lammas answered 9/2, 2009 at 15:15 Comment(7)
thanks for your reply! your first remark does indeed explain why i see no effect... is there any way i can test the error handling locally before uploading to the server? how? my project compiles, but i am deliberately throwing runtime exceptions to test error handling.Cytaster
Hi again. I have now tried customErrors="On" with and without a default redirect, customErrors="Off", removing the node completely, and setting debug="false" - and the only difference is what info is shown on the red-toned error page... Is it debugging or using Cassini that causes the problem?Cytaster
When you say Cassini do you mean the ASP.NET development server? They're not the same, and I don't know what Cassini does.Lammas
Yes, that's what I mean. The thing that runs by default when I click "play" in VS =) I have learned that they in fact are the same (in WROX's "ASP.NET 2.0 for Beginners", if i'm not mistaken...). Will debugging in IIS solve the problem?Cytaster
I have now configured the site to run in IIS instead of on the ASP.NET Development Server, but with no success. Any ideas here?Cytaster
Given what you have said, it's difficult to imagine that Web.config is being configured correctly. It is really the only explanation for the behavior you are seeing. My suggestion would be to find an example of a site that works, and compare it with what you are doing.Lammas
In my own case I had to ensure that debug was turned off and that custom errors were turned on before I could test the error handling properly.Favouritism
P
4

You need to specify what page to redirect to as well.

<customErrors mode="On" defaultRedirect="Error.aspx" />

EDIT: Sorry the /Shared/ part should not bet there but you need to tell MVC which page to send the user to with Error.aspx. Then the default route looks for something called Error.aspx in shared.

It was very late! :) I guess that's why someone gave me a minus for the answer! :) At least it works here mate!

Polis answered 8/2, 2009 at 2:27 Comment(3)
Also, it shouldn't be necessary according to this article weblogs.asp.net/scottgu/archive/2008/07/14/… (which is the most recent i have been able to find, although it's only on PV4...)Cytaster
I read that article but for me the error isnt handled unless I say where to redirect to.Polis
In my web.config i write <customErrors mode="On" defaultRedirect="Error.aspx"></customErrors>, and when an exception is thrown I get a 404 on this url: localhost:52812/Error.aspx?aspxerrorpath=/Program/EditCytaster
G
3

I got the same issue and it took me two full days to figure it out finally. It turned out to be that I got an error in Site.Master page, and the Error.aspx used this same master page as all other pages. Obviously the Error.aspx couldn't deal with such situation.

My solution is to create a specific Error.master page that is lightweight and does not include any model data. Additionaly I created a static Error.htm in case an error occurs from Error.aspx. The Web.config setting is as follows:

<customErrors mode="On">
    <error statusCode="500" redirect="Error.htm" />
</customErrors>

Hope it helps.

Girdle answered 4/5, 2009 at 22:41 Comment(2)
My Error.aspx page was also throwing its own exception. That's a pain to debug!Taphole
The Error.htm seems to return an HTTP response status code of 200 rather than 500 as it should!Taphole
H
0

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 %>
Hanan answered 27/7, 2009 at 14:26 Comment(0)
D
0

To get around the 404 problem when the Error.aspx was supposed to be shown, I had to exclude the Error.aspx from the httpHandler section which prevented any views from being accessed directly (around the mvc 2 framework). I did this by putting Error.aspx in an 'Error' subfolder and putting a web.config in this subfolder with a <remove path="*" verb="*" /> in the httpHandlers section. My version of this problem (and its solution) may be specific to MVC 2.

Remember to update the defaultRedirect reference, when you move Error.aspx :)

Dimorphous answered 29/9, 2010 at 9:29 Comment(0)
N
0

I tried the above suggestions but nothing worked for me. What did the trick was removing this line from my actions within my error controller.

Response.StatusCode = (int)HttpStatusCode.NotFound;

I was pulling my hair out since IIS error messages kept intercepting my error handling. And while its not ideal since I want to provide that status code in my response, I found that removing it prevented IIS 7+ from interfering with my error handling.

DaTribe

Nitrobenzene answered 11/7, 2011 at 7:32 Comment(1)
If you want to use this, and you can, you need to set Response.TrySkipIisCustomErrors = true first.Borisborja

© 2022 - 2024 — McMap. All rights reserved.