Ejemplo n.º 1
0
        public override void OnException(HttpActionExecutedContext actionContext)
        {
            var context = new HttpContextWrapper(HttpContext.Current);

            actionContext.Response = new HttpResponseMessage(HttpStatusCode.InternalServerError);
            var resex    = new HttpResponseMessage(HttpStatusCode.InternalServerError);
            var res      = new ErrResponsePoco();
            var entities = (actionContext.Request.GetDependencyScope().GetService(typeof(WutNuContext)) as WutNuContext);

            res.DbErrorId = AddErrorLogItem(actionContext.Exception, context, entities);

            context.ClearError();
            resex.Content = new ObjectContent(typeof(ErrResponsePoco), res, new JsonMediaTypeFormatter());

            throw new HttpResponseException(resex);
        }
Ejemplo n.º 2
0
        protected void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs
            var context    = new HttpContextWrapper(Context); //((MvcApplication)sender).Context;
            var action     = "Index";
            var statusCode = 500;

            var currentController = " ";
            var currentAction     = " ";
            var currentRouteData  = RouteTable.Routes.GetRouteData(context);

            if (currentRouteData != null)
            {
                currentController = currentRouteData.Values["controller"]?.ToString() ?? " ";
                currentAction     = currentRouteData.Values["action"]?.ToString() ?? " ";
            }

            // Get the exception object.
            var exc = Server.GetLastError();

            if (exc == null)
            {
                return;
            }

            // Clear the error from the server
            Server.ClearError();

            // Handle HTTP errors
            if (exc.GetType() == typeof(HttpException))
            {
                // The Complete Error Handling Example generates
                // some errors using URLs with "NoCatch" in them;
                // ignore these here to simulate what would happen
                // if a global.asax handler were not implemented.
                if (exc.Message.Contains("NoCatch") || exc.Message.Contains("maxUrlLength"))
                {
                    return;
                }

                //Redirect HTTP errors to HttpError page
                var httpEx = exc as HttpException;

                if (httpEx != null)
                {
                    switch (httpEx.GetHttpCode())
                    {
                    case 404:     // Redirect to 404:
                        action     = "NotFound";
                        statusCode = 404;
                        break;

                    // others if any
                    default:
                        statusCode = httpEx.GetHttpCode();
                        break;
                    }
                }
            }

            context.ClearError();
            context.Response.Clear();
            context.Response.StatusCode             = statusCode;
            context.Response.TrySkipIisCustomErrors = true;
            if (!context.Request.IsAjaxRequest())
            {
                context.Response.ContentType = "text/html";
            }

            var controller = new ErrorsController();
            var routeData  = new RouteData();

            routeData.Values["controller"] = "Errors";
            routeData.Values["action"]     = action;

            controller.ViewData.Model = new HandleErrorInfo(exc, currentController, currentAction);
            ((IController)controller).Execute(new RequestContext(context, routeData));
        }
Ejemplo n.º 3
0
Archivo: Global.cs Proyecto: zce/micua
        /// <summary>
        /// 程序错误事件
        /// </summary>
        /// <remarks>
        ///  2013-11-23 22:33 Created By iceStone
        /// </remarks>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void Application_Error(object sender, EventArgs e)
        {
#if !DEBUG
            if (this.Context.IsDebuggingEnabled || this.Context.Request.IsLocal)
            {
                return;
            }
            var exception = Server.GetLastError() as HttpException;
            if (exception != null)
            {
                var       httpContext    = new HttpContextWrapper(this.Context);
                RouteData routeData      = RouteTable.Routes.GetRouteData(httpContext);
                var       controllerName = (string)routeData.Values["controller"];
                var       actionName     = (string)routeData.Values["action"];
                var       model          = new HandleErrorInfo(exception, controllerName, actionName);
                var       controller     = new SharedController();
                controller.ViewData.Model      = model;
                routeData.Values["controller"] = "Shared";
                routeData.Values["action"]     = "Error";
                routeData.Values["status"]     = exception.GetHttpCode();
                httpContext.ClearError(); //httpContext.Response.Clear();
                httpContext.Response.ContentType = "text/html; charset=utf-8";
                httpContext.Response.StatusCode  = exception.GetHttpCode();
                ((IController)controller).Execute(new RequestContext(httpContext, routeData));
            }
#endif
            //var result = new ViewResult
            //{
            //    ViewName = Config.GetString("default_error_view", "Error"),
            //    MasterName = Config.GetString("default_error_layout", "_Layout"),
            //    ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
            //    //ViewEngineCollection = new ViewEngineCollection()
            //};
            //LogHelper.WriteErrorLog(ex.Message,ex); //记录日志信息
            //var httpStatusCode = (ex is HttpException) ? (ex as HttpException).GetHttpCode() : 500; //这里仅仅区分两种错误
            //var httpContext = ((MvcApplication)sender).Context;
            //httpContext.ClearError();
            //httpContext.Response.Clear();
            //httpContext.Response.StatusCode = httpStatusCode;
            //var shouldHandleException = true;
            //HandleErrorInfo errorModel;

            //var routeData = new RouteData();
            //routeData.Values["controller"] = "Shared";

            //switch (httpStatusCode)
            //{
            //    case 404:
            //        routeData.Values["action"] = "Error";
            //        errorModel = new HandleErrorInfo(new Exception(string.Format("No page Found", httpContext.Request.UrlReferrer), ex), "Utility", "PageNotFound");
            //        break;

            //    default:
            //        routeData.Values["action"] = "Error";
            //        Exception exceptionToReplace = null; //这里使用了EntLib的异常处理模块的一些功能
            //        shouldHandleException = ExceptionPolicy.HandleException(ex, "LogAndReplace", out exceptionToReplace);
            //        errorModel = new HandleErrorInfo(exceptionToReplace, "Utility", "Error");
            //        break;
            //}

            //if (shouldHandleException)
            //{
            //    var controller = new UtilityController();
            //    controller.ViewData.Model = errorModel; //通过代码路由到指定的路径
            //    ((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData));
            //}
        }