Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

asp.net mvc - attribute does not seem to act at all

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.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...