开发者

ASP.Net MVC Custom Error handling via Action Filter Attributes

I am trying to implement Custom Error handling via Action Filter Attributes.

My code is as follows:

[HandleError (Order = 2)]
[HandleError (Order = 1, ExceptionType = typeof(NullReferenceException), View = "CustomError")]
public class ArticlesController : Controller
{

    public object OhDearACrash()
    {
        throw new Exception("Oh Dear");
    }

    public ActionResult NullRefCrash()
    {
        throw new NullReferenceException();
    }

    public ActionResult ThrowNotImplemented()
    {
        throw new NotImplementedException();
    }

OhDearACrash and ThrowNotImplemented are both picked up by [HandleError] which renders the error message via Error.aspx located in Views/Shared.

For example with OhDearACrash:

Message = <%= ViewData.Model.Exception.Message %>

renders

Message = Oh Dear

NullRefCrash is picked up by the [HandeError] that deals with an ExceptionType = typeof(NullReferenceException).

When CustomError attempts to render the error message using

Message = <%= ViewData.Model.Exception.Message %>

the ViewData.Model is null and an exception is raised

System.NullReferenceException was unhand开发者_开发技巧led by user code
Message="Object reference not set to an instance of an object."

To create CustomError.aspx I copied Error.aspx and pasted to my newly created Views/Error and renamed to CustomView.aspx.

Since Error.aspx and CustomError.aspx are essentially the same how is this occuring?

Edit:

I created a Test Project that only contains the above and the CustomError.aspx view works perfectly fine - Is there anyway to debug my existing project to find the issue?


I just tried this on ASP.NET MVC 1.0 and I'm getting the correct behavior.

Is it possible that you have another filter somewhere else that is running and changing the error somehow?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜