상속: System.Configuration.ConfigurationSection
예제 #1
0
        internal static CustomErrorsSection GetSettings(HttpContext context, bool canThrow)
        {
            CustomErrorsSection ce            = null;
            RuntimeConfig       runtimeConfig = null;

            if (canThrow)
            {
                runtimeConfig = RuntimeConfig.GetConfig(context);
                if (runtimeConfig != null)
                {
                    ce = runtimeConfig.CustomErrors;
                }
            }
            else
            {
                runtimeConfig = RuntimeConfig.GetLKGConfig(context);
                if (runtimeConfig != null)
                {
                    ce = runtimeConfig.CustomErrors;
                }
                if (ce == null)
                {
                    if (_default == null)
                    {
                        _default = new CustomErrorsSection();
                    }

                    ce = _default;
                }
            }

            return(ce);
        }
예제 #2
0
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            System.Configuration.Configuration configuration =
                System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/");

            System.Web.Configuration.CustomErrorsSection section =
                (CustomErrorsSection)configuration.GetSection("system.web/customErrors");

            var inner            = actionExecutedContext.Exception.InnerException;
            var exceptionMessage = (inner == null) ? actionExecutedContext.Exception.Message : inner.Message;

            CustomErrorsMode mode = section.Mode;

            if (mode == CustomErrorsMode.Off)
            {
                exceptionMessage = actionExecutedContext.Exception.StackTrace.ToString();
            }
            // TODO Alvaro implementar log
            // Por cada excepción no controlada devolvemos un error 500 con una lista de errores.

            log.Error("ERROR: " + exceptionMessage);
            actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(HttpStatusCode.InternalServerError,
                                                                                          new ErrorResult {
                Error = exceptionMessage
            });
            Task.FromResult <object>(null);
            //actionExecutedContext.Response.Headers.Add("X-Error", actionExecutedContext.Exception.Message);
        }
예제 #3
0
 internal static CustomErrorsSection GetSettings(HttpContext context, bool canThrow)
 {
     CustomErrorsSection customErrors = null;
     RuntimeConfig lKGConfig = null;
     if (canThrow)
     {
         lKGConfig = RuntimeConfig.GetConfig(context);
         if (lKGConfig != null)
         {
             customErrors = lKGConfig.CustomErrors;
         }
         return customErrors;
     }
     lKGConfig = RuntimeConfig.GetLKGConfig(context);
     if (lKGConfig != null)
     {
         customErrors = lKGConfig.CustomErrors;
     }
     if (customErrors != null)
     {
         return customErrors;
     }
     if (_default == null)
     {
         _default = new CustomErrorsSection();
     }
     return _default;
 }
예제 #4
0
		public void Defaults ()
		{
			CustomErrorsSection c = new CustomErrorsSection ();

			Assert.IsNull (c.DefaultRedirect, "A1");
			Assert.IsNotNull (c.Errors, "A2");
			Assert.AreEqual (CustomErrorsMode.RemoteOnly, c.Mode, "A3");
		}
 public static void HandleError(HttpServerUtilityBase server, HttpResponseBase response,
                                CustomErrorsSection customErrorsSection)
 {
     CustomError customError = GetCustomError(server.GetLastError(), customErrorsSection);
     server.ClearError();
     response.Clear();
     response.WriteFile(customError.Redirect);
     response.StatusCode = customError.StatusCode;
 }
예제 #6
0
 protected override void Reset(ConfigurationElement parentElement)
 {
     base.Reset(parentElement);
     CustomErrorsSection section = parentElement as CustomErrorsSection;
     if (section != null)
     {
         this.basepath = section.basepath;
     }
 }
예제 #7
0
        protected override void Reset(ConfigurationElement parentElement)
        {
            base.Reset(parentElement);
            CustomErrorsSection parent = parentElement as CustomErrorsSection;

            if (parent != null)
            {
                basepath = parent.basepath;
            }
        }
예제 #8
0
        private RouteData GetErrorRouteData(HttpException httpException, CustomErrorsSection config)
        {
            var statusCode = httpException.GetHttpCode().ToString(CultureInfo.InvariantCulture);

            var routeData = RouteInfo.GetRouteDataByUrl(config.Errors[statusCode] != null
                                             ? config.Errors[statusCode].Redirect
                                             : config.DefaultRedirect);

            return routeData;
        }
        private static CustomError GetCustomError(Exception exception, CustomErrorsSection section)
        {
            var httpException = exception as HttpException;
            var defaultError = section.Errors.Get("500");

            if (httpException == null)
                return defaultError;

            var customError = section.Errors.Get(httpException.GetHttpCode().ToString());

            return customError ?? defaultError;
        }
 internal static CustomErrorsSection GetSettings(HttpContext context, bool canThrow)
 {
     CustomErrorsSection customErrors = null;
     RuntimeConfig lKGConfig = null;
     if (canThrow)
     {
         lKGConfig = RuntimeConfig.GetConfig(context);
         if (lKGConfig != null)
         {
             customErrors = lKGConfig.CustomErrors;
         }
         return customErrors;
     }
     lKGConfig = RuntimeConfig.GetLKGConfig(context);
     if (lKGConfig != null)
     {
         customErrors = lKGConfig.CustomErrors;
     }
     if (customErrors != null)
     {
         return customErrors;
     }
     if (_default == null)
     {
         _default = new CustomErrorsSection();
     }
     return _default;
 }
예제 #11
0
 private string GetErrorRedirect(int errorCode, bool isLocal)
 {
     if (!customErrorsInit)
     {
         var map = new ExeConfigurationFileMap()
         {
             ExeConfigFilename = Path.Combine(_server.PhysicalPath, "Web.config")
         };
         var cfg = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
         customErrorsSection = cfg.GetSection("system.web/customErrors") as CustomErrorsSection;
         customErrorsInit = true;
     }
     if (customErrorsSection != null &&
         customErrorsSection.Mode != CustomErrorsMode.Off &&
         (!isLocal || (isLocal && customErrorsSection.Mode != CustomErrorsMode.RemoteOnly)))
     {
         var customError = customErrorsSection.Errors[errorCode.ToString()];
         return (customError != null ? customError.Redirect : customErrorsSection.DefaultRedirect);
     }
     return null;
 }
예제 #12
0
        /// <summary>
        /// 根据CustomeErrors配置获取错误处理页面URL
        /// </summary>
        /// <param name="customErrorsSection">错误配置节</param>
        /// <returns>错误处理页面URL</returns>
        public static string GetCustomErrorURL(CustomErrorsSection customErrorsSection)
        {
            string redirectURL = string.Empty;

            if (customErrorsSection.Mode == CustomErrorsMode.On || (customErrorsSection.Mode == CustomErrorsMode.RemoteOnly && !HttpContext.Current.Request.IsLocal))
            {
                redirectURL = customErrorsSection.DefaultRedirect;
                CustomError error = customErrorsSection.Errors[Convert.ToString(HttpContext.Current.Response.StatusCode)];
                if (null != error)
                    redirectURL = error.Redirect;
            }
            return redirectURL;
        }
예제 #13
0
        private ConfigurationCustomErrorsModel ProcessCustomErrors(CustomErrorsSection customErrorsSection)
        {
            if (customErrorsSection == null)
            {
                return null;
            }

            var result = new ConfigurationCustomErrorsModel();
            result.DefaultRedirect = customErrorsSection.DefaultRedirect;
            result.RedirectMode = customErrorsSection.RedirectMode.ToString();
            result.Mode = customErrorsSection.Mode.ToString();

            var errorsSection = customErrorsSection.Errors;
            if (errorsSection != null)
            {
                var resultErrors = new List<ConfigurationCustomErrorsErrorModel>();
                foreach (CustomError error in errorsSection)
                {
                    var resultError = new ConfigurationCustomErrorsErrorModel();
                    resultError.Redirect = error.Redirect;
                    resultError.StatusCode = error.StatusCode;

                    resultErrors.Add(resultError);
                }

                result.Errors = resultErrors;
            }

            return result;
        }
예제 #14
0
 private bool UseCustomErrors(CustomErrorsSection config)
 {
     return config.Mode == CustomErrorsMode.On ||
            (config.Mode == CustomErrorsMode.RemoteOnly && Request.Url.Host != "localhost");
 }
예제 #15
0
 private static Func<IHttpHandler> FindHttpHandler(CustomErrorsSection customErrorsSection, string customErrorId)
 {
     var customErrorsSectionSyn = new CustomErrorsSectionSyn(customErrorsSection);
     var customErrorSyn = new CustomErrorSyn(customErrorsSection.Errors[customErrorId]);
     Type httpHandlerType = null;
     try
     {
         httpHandlerType = customErrorsSectionSyn.DefaultUrlRoutingType;
         if (customErrorSyn.UrlRoutingType != null)
             httpHandlerType = customErrorSyn.UrlRoutingType;
     }
     catch { }
     return (httpHandlerType == null ? (Func<IHttpHandler>)null : () => (IHttpHandler)Activator.CreateInstance(httpHandlerType));
 }
예제 #16
0
 public CustomErrors(CustomErrorsSection customErrorsSection)
 {
     _customErrorsSection = customErrorsSection;
 }
 public CustomErrorsSectionWrapper(CustomErrorsSection customErrorsSection) {
     Debug.Assert(customErrorsSection != null);
     _customErrorsSection = customErrorsSection;
 }
        internal static CustomErrorsSection GetSettings(HttpContext context, bool canThrow) {
            CustomErrorsSection ce = null;
            RuntimeConfig runtimeConfig = null;
            if (canThrow) {
                runtimeConfig = RuntimeConfig.GetConfig(context);
                if (runtimeConfig != null) {
                    ce = runtimeConfig.CustomErrors;
                }
            }
            else {
                runtimeConfig = RuntimeConfig.GetLKGConfig(context);
                if (runtimeConfig != null) {
                    ce = runtimeConfig.CustomErrors;
                }
                if (ce == null) {
                    if (_default == null) {
                        _default = new CustomErrorsSection();
                    }

                    ce = _default;
                }
            }

            return ce;
        }