1
votes

I have a custom attribute with the following code:

public override void OnException(ExceptionContext filterContext)
    {
        if (filterContext != null && filterContext.HttpContext != null && filterContext.Exception != null)
        {
            //If customError is Off, then AI HTTPModule will report the exception
            if (filterContext.HttpContext.IsCustomErrorEnabled)
            {   
                      //Log to AI
            }
        }

        base.OnException(filterContext);
    }

So whenever an exception occurs, it runs through this attribute, which will log the exception in Application Insights.

My problem is that the base.OnException is redirecting to a default "Error" view, I deleted the default Error view that was located in /Shared/Error.cshtml, and created my own error view. How do I get it to redirect to my custom error view instead of the default one? Right now it is throwing view errors since it cannot find the default error view.

System.InvalidOperationException The view 'Error' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/Shared/Error.aspx ~/Views/Shared/Error.ascx ~/Views/Shared/Error.cshtml ~/Views/Shared/Error.vbhtml

Any insights to this would be greatly appreciated. Thanks!

2
Why do you want to use another file? Just change the content of default Error.cshtmlReza Aghaei
I am saving that for my last option, the reason I don't want to do that is because we have too many usage of the new custom error page that I'd have to change it everywhere. I also considered adding it and including the new error page as a partial, but didn't like that option either.Ken Huynh

2 Answers

3
votes

Consider these options:

1- You don't need to change the Error.cshtml file name, you can change the content of the file.

2- You can pass the view name to your custom attribute this way and decorate the action or controller with this attribue:

[CustomHandleError(View="SomeView")]

3- If you don't want to pass the view name and you want to have the name in a central locaton, you can write your custom attribute this way and decorate your action or controllers with [CustomHandleError]:

public class CustomHandleError:HandleErrorAttribute
{
    public CustomHandleError():base()
    {
        View = "SomeView";
    }
    //rest of your implementation
}
0
votes

You can update the redirect result in the filter context

filterContext.Result = this.RedirectToAction("YourErrorView", "YourErrorController");

 base.OnException(filterContext)