views:

33

answers:

2

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.

[HandleError]
public class DebugController : Controller
{

    public ActionResult Index()
    {
        throw new Exception("Make something go wrong to trigger custom errors");
        return View();
    }

}

namespace FilmFestWeb.Controllers {

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.   Please contact Corporate Applications by creating a Service Now ticket or request help from your local help desk.";
        }
        else
        {
            ViewData["error"] = TempData["error"];
        }

        return View();
    }

    public ActionResult NoAccess()
    {
        ViewData["error"] = "You are not authorized to acccess this application.   Please contact Corporate Applications by creating a Service Now ticket or request help from your local help desk.";
        return 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>

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

+1  A: 

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

Tommy
+1  A: 

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.

Kyle LeNeau