开发者

MVC3 HandleError doesn't show error page

I've been following this guide for the HandleError attribute: blogs.msdn.com which I use like this (AccountController):

[HandleError(View="ErasErrorPage")]
        public ActionResult Index()
        {
            ViewBag.admins = _accountMapper.GetAdmins(false);
            ViewBag.members = _accountMapper.GetMembers(false);
            ViewBag.Countries = _countryMapper.GetCountries();

            return View();
        }

This code throws an exception because _accountMapper.GetAdmins(false) fails because of a System.Data.EntityException.

I've put the ErasErrorPage view in my Shared folder and I've added <customErrors mode="On"/> but the ErasErrorPage does not show up. All I get when the error occurs is a yellow screen of death saying:

MVC3 HandleError doesn't show error page

Obviously, setting the mode to "Off" or "RemoteOnly" doesn't solve the problem.

Anyone has an idea why this doesn't work?

EDIT

If I surf directly to http://localhost:3527/Account/Index, I do get the correct Er开发者_高级运维asErrorPage, but I don't want that. I want the website to automaticly redirect to that page when an exception is thrown somewhere. Is this possible?

EDIT2

I've put the [HandleError(View="ErasErrorPage")] attribute right before every single Public ActionResult methodName() { ... } method, and I still get the Yellow Screen of Death saying I need to change the mode to "Off" or "RemoteOnly"...


Make sure that the ErasErrorPage view doesn't itself throw an exception. Here are the steps I did and which worked fine for me:

  1. Create a new ASP.NET MVC 3 Project using the default Visual Studio wizard
  2. Add ~/Views/Shared/ErasErrorPage.cshtml with the following content:

    @{
        Layout = null;
    }
    <!DOCTYPE html>
    <html>
    <head>
        <title>ErasErrorPage</title>
    </head>
    <body>
        <div>
            OOPS
        </div>
    </body>
    </html>
    
  3. Modify HomeContoller to look like this:

    public class HomeController : Controller
    {
        [HandleError(View = "ErasErrorPage")]
        public ActionResult Index()
        {
            throw new Exception("oops");
        }
    }
    
  4. In web.config put:

    <customErrors mode="On" />
    
  5. Navigate to /Home/Index => the custom error page is displayed as expected


Since this doesn't work at all, I've found a very good alternative:

Custom error pages on asp.net MVC3

I also found out why it failed:

We were trying to connect to the database before we tried rendering views. The HandleError attribute won't even be triggered by that (I think). The method above does do that. It handles any exception, anywhere.


It does work, I have a BaseController with the attribute [HandleError(View = "Error")] and an Error view in Shared with a dynamic model (should be HandleErrorInfo) and at the Index action in the HomeController I am throwing an exception:

throw new Exception("Test exception");

The error view is then rendered and displays the correct information.

ErrorView:

<div class="errorMessage">
    <h1 class="errorHeader">@Model.Exception.GetType().Name</h1>
    <cite class="errorAction">thrown in @Model.ControllerName @Model.ActionName</cite>
    <p class="errorMessage">thrown in @Model.Message</p>
</div>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜