Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have started an MVC 3 template project in VS10 and modified global.asax.cs as such:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute { ExceptionType = typeof(DivideByZeroException), View = "DivideByZeroException", Order = 1 });
    filters.Add(new HandleErrorAttribute { View = "AllOtherExceptions", Order = 2 });
}

To web.config I added:

<customErrors mode="On">

Then created the corresponding views and finally added a DivideByZero-throw to one of the actions.

The result: The view AllOtherExceptions is rendered.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
747 views
Welcome To Ask or Share your Answers For Others

1 Answer

Much though I hate to disagree with anything Darin says, he is wrong on this one.

There is no problem with setting the properties (that's the way you are supposed to do it).

The only reason your original code didn't work as expected is because you have the Order set wrong.

See MSDN:

The OnActionExecuting(ActionExecutingContext), OnResultExecuting(ResultExecutingContext), and OnAuthorization(AuthorizationContext) filters run in forward order. The OnActionExecuted(ActionExecutedContext), OnResultExecuting(ResultExecutingContext), and OnException(ExceptionContext) filters run in reverse order.

So your generic AllOtherExceptions filter needs to be the lowest Order number, not the highest.

Hopefully that helps for next time.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...