0

In an MVC application, how do we show the standard MVC error page as well as handle exception using the Microsoft Exception Handling Application Block?

I have updated my web.config file with

This redirects to the Trouble view upon an exception. But the exception is no longer handled by the Exception Handling Application Block (code below). ExceptionPolicy.HandleException(ex, "AllExceptions", out errorToThrow);

How can I show the Error view as well as handle Exception? I do not want to use ELMAH.

2 Answers 2

1

Use Log4net and a HttpHandler for logging and error handling. See the following sample code. And decorate the controllers with the same ExceptionHandler attribute.( [ExceptionHandler] public class BillsController : Controller { })

public class ExceptionHandler : HandleErrorAttribute
{
    private static readonly ILog log = LogManager.GetLogger(typeof(ExceptionHandler));

    public override void OnException(System.Web.Mvc.ExceptionContext filterContext)
    {
        log.Debug("******** ExceptionHandler.OnException() *****************");

        base.OnException(filterContext);
        Exception ex = filterContext.Exception;
        if (filterContext.Exception is BusinessException)
        {
            log.Debug("<<<<Inside On BusinessException....>>>>" + DateTime.Now);
            //log.Debug("*** Error Getting From **** " + LOGIN_DETAILS.LoginUserName +
            //          "(UserId = " + LOGIN_DETAILS.LoginUserID + ")" + "Time =" + DateTime.Now);


            BusinessException _BusinessException =
                (BusinessException)filterContext.Exception;

            StringBuilder errormsg = new StringBuilder();
            foreach(MessageInfo msg in _BusinessException.ErrorMessageList)
            {
                errormsg.AppendLine(msg.LocalizedMessage);
            }

            log.Error("<<<<--------BusinessException-------->>>> : Exception Details -----"+ errormsg.ToString());
            //filterContext.ExceptionHandled = true;

            filterContext.ExceptionHandled = true;
            RequestContext rc = new RequestContext(filterContext.HttpContext, filterContext.RouteData);
            string url = RouteTable.Routes.GetVirtualPath(rc, new RouteValueDictionary(new { Controller = "Account", action = "Logout", message = filterContext.Exception.Message })).VirtualPath;
            filterContext.HttpContext.Response.Redirect(url, true);
        }
        else
        {
             log.Error("Exception Details ---- " +
               "MESSAGE: " + ex.Message +
               "\nSOURCE: " + ex.Source +
               "\\Controller: " + filterContext.Controller.ToString() +
               "\nTARGETSITE: " + ex.TargetSite +
               "\nSTACKTRACE: " + ex.StackTrace,
               ex);

            filterContext.ExceptionHandled = true;
            RequestContext rc = new RequestContext(filterContext.HttpContext, filterContext.RouteData);
            string url = RouteTable.Routes.GetVirtualPath(rc, new RouteValueDictionary(new { Controller = "Exception", action = "Default" })).VirtualPath;
            //string url = RouteTable.Routes.GetVirtualPath(rc, new RouteValueDictionary(new { Controller = "exception", action = "Default", message = filterContext.Exception.Message })).VirtualPath;
            filterContext.HttpContext.Response.Redirect(url, true);
        }

    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks amesh. Is there a way I can pass a model to the Controller Action during .HttpContext.Response.Redirect(url, true) ?
Of course.. Better you can try RedirectToAction("Action", "Controller" new{model=yourmodel}). (New questions are encouraged as a different question)
1

Change your web.config file to show custom errors, this way you can show the error page.

then for handeling the exception add this function to you global.asax:

protected void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();

    //handle the exception
}

2 Comments

Thank you middelpat. Could you also provide the code for routing to a specific Controller/Action from Global.aspx page?
if you could reward my efforts by for example upvoting i'll see if i can help you out. perhaps you should open a new question and provide me a little more information on what you're trying to acomplish

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.