public static ActionResult HandleException(HttpApplication context, Exception exception)
        {
            if (exception != null)
            {
                var httpException = exception as HttpException;
                int statusCode = httpException != null ? httpException.GetHttpCode() : 500;
                //check the statusCode against the config
                if (!Routing.StatusCodesToIgnoreFromWebConfig.Contains(statusCode))
                {
                    context.Response.Clear();
                    context.Server.ClearError();
                    context.Response.StatusCode = statusCode;

                    var routeData = CreateRoute(context, exception);

                    context.Response.TrySkipIisCustomErrors = true;

                    IController errorHandlerController = new ErrorHandlerController();
                    var rc = new RequestContext(new HttpContextWrapper(context.Context), routeData);
                    errorHandlerController.Execute(rc);
                }
            }

            return null;
        }
        protected void Application_Error()
        {
            Exception ex = Server.GetLastError();

            Context.Response.Clear();
            Context.ClearError();
            RequestContext requestContext = ((MvcHandler)Context.CurrentHandler).RequestContext;

            if (requestContext.HttpContext.Request.IsAjaxRequest())
            {
                Context.Response.Write("Ajax request failed due to server error!");
                Context.Response.End();
            }
            else
            {
                var routeData = new RouteData();
                routeData.Values["controller"] = "ErrorHandler";
                routeData.Values["action"]     = "InternalException";
                routeData.Values.Add("url", Context.Request.Url.OriginalString);
                routeData.Values.Add("exception", ex);
                //var httpException = ex as HttpException;
                //if (httpException != null)
                //{
                //	switch (httpException.GetHttpCode())
                //	{
                //		case 404:
                //			routeData.Values["action"] = "NotFound";
                //			break;
                //		case 403:
                //			routeData.Values["action"] = "GenericError";
                //			break;
                //		case 500:
                //			routeData.Values["action"] = "InternalException";
                //			break;
                //	}
                //}
                IController controller = new ErrorHandlerController();
                controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
            }
        }
        protected void Application_Error(object sender, EventArgs e)
        {
            Exception exception = Server.GetLastError();
            RouteData routeData = new RouteData();

            routeData.Values.Add("ErrorMsg", exception.Message);
            routeData.Values.Add("StackTrace", exception.StackTrace);
            routeData.Values.Add("Controller", "ErrorHandler");
            if (exception is DbException)
            {
                routeData.Values.Add("Action", "DatabaseError");
            }
            else if (exception is HttpException)
            {
                routeData.Values.Add("ExUrl", HttpContext.Current.Request.Url);
                if ((exception as HttpException).GetHttpCode() == 404)
                {
                    routeData.Values.Add("Action", "PageError");
                }
                else
                {
                    routeData.Values.Add("Action", "CommonError");
                }
            }
            else
            {
                routeData.Values.Add("Action", "CommonError");
            }
            Response.Clear();
            Server.ClearError();
            try
            {
                IController exceptionControl = new ErrorHandlerController();
                exceptionControl.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
            }
            catch (Exception)
            {
            }
        }