asp.net mvc errorhandler not showing custom error page

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 17:09:10

问题


I am trying to follow the examples in this link and this one but instead of showing the error page I get an HTTP 500 Internal server error. I have the <customErrors mode="On" /> set in the webconfig. I have even tried applying the [HandleError] filter to the controller class. I tried without as well. The Error.aspx is present in /Views/Shared/ as well so it couldn't be a case of no file found.

I threw a DivideByZero exception in my controller's action method. I want to follow that example so that I can specify a separate error page for all the actions that need them.

I am using the aspx view engine in a blank project that I created. That should not be the reason for it right?. I am also using a master page for this. Anything that I could be missing?

Thanks

Edit-Added Code

I added this code to a new project and made the web.config entry <customErrors mode="On" />

 [HandleError]
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        [HandleError]
        public ActionResult Index()
        {

            throw new DivideByZeroException();
            return View();
        }

    }

It didnt work! Then I commented that DivideByZeroException and in the aspx view just added and invalid Model.Property. In both instances I got the Internal server error. I have done everything there is to be done as per documentation. I see a lot of other people having the same problem and not being able to solve it too. Is this a bug in MVC 3?


回答1:


I think I have figured out what really was the problem. My main Home page index was in a master page. I added the same master page to the default error page that comes out of the box with Visual Studio and it worked for me.

The documentation needs to point out more clearly this important requirement,... if it indeed is one.

Another amazing revelation is that you don't need to decorate your controller classes with the [HandleError] attribute. It works without that as well for the default Error view. You may provide that attribute if you want a specific custom view for your action or controller. Like so:

[HandleError(View = "CustomError")]

...where CustomError.aspx is just another plain aspx view page in either the shared folder for the View or in the View-Controller folder itself.

Has anyone got this working without putting the error pages in a master page, where the main calling page that throws the error is in a master page?




回答2:


The HandleError filter doesn't catch all the errors. It doesn't catch exceptions that are raised outside controller actions/action filters. Also, it doesn't catch HTTP exceptions having status code other than 500.

So you have to make sure where the exception is getting thrown and you should not rely only on the HandleError to return the custom error page but you also have to set a custom error page in the customErrors section as well.

<customErrors defaultRedirect="error.htm" mode="On" 
  redirectMode="ResponseRewrite" />



回答3:


Make sure you have activated custom errors in your web.config:

<customErrors mode="On" />

Also make sure that the ~/Views/Shared/Error.aspx template is present because this is what will be rendered in case of error.



来源:https://stackoverflow.com/questions/11231165/asp-net-mvc-errorhandler-not-showing-custom-error-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!