开发者

ASP.NET MVC Exceptions not routing properly to Customerrors default route

I'm having an issue with exceptions showing app as stack traces in the page.

I thought that I had mitigated this by adding this to the web.config:

<customErrors mode="On" defaultRedirect="~/error/GenericError">
    <error statusCode="403" redirect="~/error/NoAccess" />
    <error statusCode="404" redirect="~/error/NotFound" />
</customErrors>

It works for non-existent routes, but not when a controller throws an exception. Here's the Controller logic:

[HandleError]
public class DebugController : Controller
{      
    public ActionResult Index()
    {
        throw new Exception("** Testing custom error Handling **");
        return View();
    }
}



public class ErrorController : Controller
{
    //
    // GET: /Error/

    public ActionResult NotFound()
    {
        ViewData["error"] = "That page does not exist.";
        return View();
    }

    public ActionResult GenericError()
    {
        if (null == TempData["error"]))
        {
            ViewData["error"] = "We're sorry, but an error has occurred.";
        }
        else
        {
            ViewData["error"] = TempData["error开发者_运维知识库"];
        }

        return View();
    }

    public ActionResult NoAccess()
    {
        ViewData["error"] = "You are not authorized to access application";
        return View();
    }

}

And here's the View:

<%@ Page Title="" Language="C#"
    MasterPageFile="~/Views/Shared/CenterContentNoSidebar.Master"
    Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
GenericError
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>An Error has occurred</h2>
    <br />
    <%= ViewData["error"] %>
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="NavContent" runat="server">
</asp:Content>

Do I have to put something in the Global.asax to make this work?


So The HandleError Attribute is actually handling the error and trying to return an Error.aspx view from the controllers view folder or the shared location. The customErrors web.config section is never being hit, and since the page might not exist the yellow screen of death is being displayed anyways. The non-existent routes is working cause IIS is throwing a 404 error and has no context of a controller or HandleError attribute. If you want to handle the errors yourself I would suggest removing the HandleError attributes and let the customErrors some through. From your error controller though you would have to grab the last exception though and do something with it. Here is a good reference on what I am talking about a little.

http://blog.dantup.com/2009/04/aspnet-mvc-handleerror-attribute-custom.html

It is kind of a one or the other type of thing, HandleError attribute or customErrors web.config section.


Your seeing this behavior because of the [HandleError] that you placed on your top line. Please see this qusetion/answer on Stack for more information on the [HandleError] attribute.

StackOverflow Handle Error Q&A

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜