简体   繁体   中英

Asp.Net MVC 3 App HandleError not working

Okay, I think I've ready every post on StackOverflow regarding this and still no luck.

I've confirmed in my global.asax that the HandleErrorAttribute filter is being registered. I've set customErrors mode="On" in my web.config. I've changed Layout=null to Layout="~/Views/Shared/_Layout.cshtml" in my Error.cshtml file.

I then added a throw Exception() in my Account controller's Index action and browse to localhost:5050/Account/Index but I wind up with the standard YSOD error stack stuff.

I've even tried the following (although from my understanding it shouldn't matter):

  1. Add [HandleError] and [HandleError(View="Error")] to my controller.
  2. Add defaultRedirect="Error" to my customErrors in web.config

What else can I do? Is there a way to tell what's going on in the HandleError filter or to tell if the Error View is being generated at all?

Thanks for your help.

PS Incidentally, I'm using VS2010, IE9 & Chrome

I cannot reproduce the problem. It is very likely that there's an error either in your _Layout.cshtml or Error.cshtml templates. Or in some partial that those templates might be depending upon.

Steps:

  1. Create a new ASP.NET MVC 3 application using the default template
  2. Add <customErrors mode="On" /> to web.config
  3. Add throw new Exception(); in the body of the LogOn action on the Account controller.
  4. Run the site and navigate to /account/logon
  5. The Error.cshtml view is correctly rendered

The YSOD you got normally contains an exception stacktrace that should help you isolate the problem. So to try to isolate the problem, try eliminating any potential code in those templates that might cause the problem. Try putting Layout = null in the Error template to eliminate the layout. And if there's an error (as I suspect) in either your Layout or Error templates you will end up with this:

在此输入图像描述

If you still are unable to isolate the problem try subscribing to the Application_Error event in your Global.asax and see if you can get more information about the exception.

Okay, ignorance is not always bliss ... sometimes being ignorant is just plain frustrating!

It turns out I was editing the web.config located in my Views folder instead of the web.config for my site like I thought I was.

Once I added customErrors mode="On" to my real web.config, things worked great.

Thanks to everyone that tried to help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM