2

Basically when my app has an unhandled exception I want it to be recorded as an unhandled exception by IIS (so that it can be seen on Event Viewer, for example) but I also want to route the user to an error controller.

I've overridden the OnException method of my controller in order to route users to a custom error page. The crux of the problem is this code:

    protected override void OnException(ExceptionContext filterContext)
    {
        filterContext.Result = RedirectToAction("GeneralError", "Error", new{ routeValueA = "some value", routeValueB = "some other value"});
        filterContext.ExceptionHandled = false;
    }

My Problem is this: If I set filterContext.ExceptionHandled = false then I get a yellow screen of death instead of being rerouted to my error handling controller. If i set filterContext.ExceptionHandled = true then I get rerouted, but the exception is not recorded as an unhandled exception.

I know I can set a static error page using the web.config, but I don't want to do this, because then I can't use route values to dynamically send data to my Error Controller.

Can I succesfully set a result to my filterContext.Result without marking filterContext.ExceptionHandled= true?

Slappywag
  • 1,143
  • 1
  • 17
  • 27

2 Answers2

2

try following from this source

protected override void OnException(ExceptionContext filterContext)
{
    if (filterContext.ExceptionHandled)
    {
        return;
    }
    filterContext.Result = new ViewResult
    {
        ViewName = "~/Views/Shared/Error.aspx"
    };
    filterContext.ExceptionHandled = true;
}

Or You can even try this too

set custom errors in web.config as below:

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

Your ErrorController:

public class ErrorController : Controller
{
    public ViewResult Index()
    {
        return View("Error");
    }
    public ViewResult NotFound()
    {
        Response.StatusCode = 404;  //you may want to set this to 200
        return View("NotFound");
    }
    public ViewResult UnauthorizedAccess()
    {
        Response.StatusCode = 404;  //you may want to set this to 200
        return View("UnauthorizedAccess");
    }
}

Register HandleErrorAttribute as a global action filter in the FilterConfig class as follows:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
     filters.Add(new CustomHandleErrorAttribute());
     filters.Add(new AuthorizeAttribute());
}

UPDATE

I suggest you to read this answer as it gives the complete detail to the question you have asked and I hope you will find a good solution there!!

Community
  • 1
  • 1
Guruprasad J Rao
  • 29,410
  • 14
  • 101
  • 200
  • Thanks for the answer, but it doesn't solve my problem: I'd like to redirect dynamically (i.e. not with web.config), but also have the exception marked as not handled - so that IIS picks it up as an unhandled exception. I've edited the question to make it a little clearer – Slappywag May 05 '15 at 12:22
  • @ChrisRedhead.. Check the **Updated** part.. :) – Guruprasad J Rao May 05 '15 at 12:37
  • 1
    Thanks for that. It seems to me that it's not possible to do what I want to do - i.e. essentially handle an exception but let IIS think it's unhandled - but I can't find a definitive source to confirm. I'll leave this open for a day or two to see if anybody has one, then I'll accept this as the most helpful answer. – Slappywag May 06 '15 at 15:07
  • Great @ChrisRedhead.. In the mean time I'll try to fetch any possible solution to your problem if available.. Happy coding.. :) – Guruprasad J Rao May 06 '15 at 17:15
0

I believe what you are looking for is Custom Error Pages that you can set in your web.config file and tell IIS to redirect to a custom error page instead of the default (as you called it yellow page of death :) ) for any unhandled exceptions.

This might help : How to make custom error pages work in ASP.NET MVC 4

Community
  • 1
  • 1
Beatles1692
  • 5,214
  • 34
  • 65
  • Thanks for the answer, but it doesn't solve my problem. I want to be able to handle exceptions dynamically, but still have the exception marked as unhandled. I've updated the question to make it clearer – Slappywag May 05 '15 at 12:24