Beispiel #1
0
        protected void Application_Error(object sender, EventArgs e)
        {
            //Log Exceptions
            Exception unhandledException = Server.GetLastError();


            if (unhandledException != null && unhandledException.GetBaseException() != null)
            {
                unhandledException = unhandledException.GetBaseException();
            }


            string systemInfo = "";
            if (HttpContext.Current != null)
            {
                systemInfo = "> > > Requested URI: " + HttpContext.Current.Request.Url.AbsoluteUri.ToString();
                systemInfo += "\n\r> > > Referrer: " + HttpContext.Current.Request.UrlReferrer;
                if (HttpContext.Current.User != null) systemInfo += "\n\r> > > Authenticated: " + HttpContext.Current.User.Identity.IsAuthenticated;
                if (HttpContext.Current.User != null) systemInfo += "\n\r> > > UserName: "******"Application_Error: " + systemInfo);


            var httpContext = ((MvcApplication)sender).Context;

            var currentRouteData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext));
            var currentController = " ";
            var currentAction = " ";

            if (currentRouteData != null)
            {
                if (currentRouteData.Values["controller"] != null && !String.IsNullOrEmpty(currentRouteData.Values["controller"].ToString()))
                {
                    currentController = currentRouteData.Values["controller"].ToString();
                }

                if (currentRouteData.Values["action"] != null && !String.IsNullOrEmpty(currentRouteData.Values["action"].ToString()))
                {
                    currentAction = currentRouteData.Values["action"].ToString();
                }
            }

            var controller = new ErrorController();
            var routeData = new RouteData();
            var action = "Index";

            if (unhandledException is HttpException)
            {
                var httpEx = unhandledException as HttpException;

                switch (httpEx.GetHttpCode())
                {
                    case 404:
                        action = "NotFound";
                        break;

                    // others if any

                    default:
                        action = "Index";
                        break;
                }
            }

            httpContext.ClearError();
            httpContext.Response.Clear();
            httpContext.Response.StatusCode = unhandledException is HttpException ? ((HttpException)unhandledException).GetHttpCode() : 500;
            httpContext.Response.TrySkipIisCustomErrors = true;
            routeData.Values["controller"] = "Error";
            routeData.Values["action"] = action;

            controller.ViewData.Model = new HandleErrorInfo(unhandledException, currentController, currentAction);
            ((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData));
        }
Beispiel #2
0
        protected void Application_EndRequest()
        {
            //Show 404 layoutet page
            if (Context.Response.StatusCode == 404)
            {
                string systemInfo = "404 not founf";
                systemInfo += "> > > Requested URI: " + HttpContext.Current.Request.Url.AbsoluteUri.ToString();
                systemInfo += "\n\r> > > Referrer: " + HttpContext.Current.Request.UrlReferrer;
                systemInfo += "\n\r> > > Authenticated: " + HttpContext.Current.User.Identity.IsAuthenticated;
                systemInfo += "\n\r> > >▶ UserName: "******"area"] = ""; // In case controller is in another area
                rd.Values["controller"] = "Error";
                rd.Values["action"] = "PageNotFound";

                IController c = new ErrorController();
                c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
            }

        }