How do I get ASP.NET MVC to honor my customErrors

2019-03-30 04:31发布

问题:

In the customErrors tag in my web.config, I am pointing to a controller. In my controller I am redirecting to an external error page that is shared by multiple applications.

<customErrors defaultRedirect="~/Error/ServerError" mode="On">

My controller:

public class ErrorController : Controller
{

    public ActionResult ServerError()
    {
        return Redirect("/Systems/ASPNETErrorHandling/ErrorPage.aspx");
    }

    public ActionResult ErrorTest()
    {
        throw new Exception("testing error handling");
    }
}

I'm calling Error/ErrorTest to test the error handling. But it always redirects to Views/Shared/Error.cshtml instead of redirecting to the controller I specified.

How do I get asp.net mvc to honor the defaultRedirect path in my customErrors settings?

UDPATE: I'm also using ELMAH and overriding the HandleErrorAttribute as described in this post. I see from .Net Reflector that the base HandleErrorAttribute is setting Error as the view. I don't think there's much I can do about it redirecting to Error.cshtml, or some other view.

回答1:

Like I said in the update to my question, I'm overriding the HandleErrorAttribute as described in this post in order to use ELMAH. The base class automatically sets Error as the view. So I just commented out base.OnException(context); and it works. I don't know if it's the best solution, but I think it should work for my purposes.