コード例 #1
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);
        }
コード例 #2
0
        public string GenerateOutput(HttpContext context, Config c)
        {
            StringBuilder sb = new StringBuilder();

            //Figure out CustomErrorsMode
            System.Configuration.Configuration configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
            CustomErrorsSection section = (configuration != null) ? section = (CustomErrorsSection)configuration.GetSection("system.web/customErrors") : null;
            CustomErrorsMode    mode    = (section != null) ? section.Mode : CustomErrorsMode.RemoteOnly;
            //What is diagnostics enableFor set to?
            DiagnosticMode dmode = c.get <DiagnosticMode>("diagnostics.enableFor", DiagnosticMode.None);
            //Is it set all all?
            bool diagDefined = (c.get("diagnostics.enableFor", null) != null);
            //Is it available from localhost.
            bool availLocally = (!diagDefined && mode == CustomErrorsMode.RemoteOnly) || (dmode == DiagnosticMode.Localhost);

            sb.AppendLine("The Resizer diagnostics page is " + (availLocally ? "only available from localhost." : "disabled."));
            sb.AppendLine();
            if (diagDefined)
            {
                sb.AppendLine("This is because <diagnostics enableFor=\"" + dmode.ToString() + "\" />.");
            }
            else
            {
                sb.AppendLine("This is because <customErrors mode=\"" + mode.ToString() + "\" />.");
            }
            sb.AppendLine();
            sb.AppendLine("To override for localhost access, add <diagnostics enableFor=\"localhost\" /> in the <resizer> section of Web.config.");
            sb.AppendLine();
            sb.AppendLine("To ovveride for remote access, add <diagnostics enableFor=\"allhosts\" /> in the <resizer> section of Web.config.");
            sb.AppendLine();
            return(sb.ToString());
        }
コード例 #3
0
        public void SetCustomErrorsMode(CustomErrorsMode mode)
        {
            Debug.WriteLine($"Setting custom error mode: <customErrors mode=\"{mode}\"/>");
            var section = (CustomErrorsSection)_webConfig.GetSection(CustomErrorsSectionName);

            // Unfortunately this doesn't immediately take effect until the app is restarted
            // which means it requires a page refresh - which often times might be good enough
            section.Mode = mode;
        }
コード例 #4
0
        static CustomErrors()
        {
            _errors = new Hashtable();
            CustomErrorsSection section = (CustomErrorsSection)WebConfigurationManager.GetSection("system.web/customErrors");

            _mode            = section.Mode;
            _redirectMode    = section.RedirectMode;
            _defaultRedirect = section.DefaultRedirect;
            foreach (CustomError e in section.Errors)
            {
                if (!string.IsNullOrEmpty(e.Redirect))
                {
                    _errors.Add(e.StatusCode, e.Redirect);
                }
            }
            _ips = Dns.GetHostAddresses("localhost");
        }
コード例 #5
0
        public void UpdateSection(
            CustomErrorsMode mode,
            CustomErrorsRedirectMode redirectMode,
            CustomErrorsRedirectType notFoundErrorRedirect,
            CustomErrorsRedirectType forbiddenErrorRedirect,
            CustomErrorsRedirectType serverErrorRedirect)
        {
            Mode = mode;
            RedirectMode = redirectMode;
            NotFoundErrorRedirect = notFoundErrorRedirect;
            ForbiddenErrorRedirect = forbiddenErrorRedirect;
            ServerErrorRedirect = serverErrorRedirect;

            _customErrorsSection.Mode = Mode.ToSystemCustomErrorsMode();
            _customErrorsSection.RedirectMode = RedirectMode.ToSystemCustomErrorsRedirectMode();

            SetCustomErrorsSectionErrors(
                _notFoundError.CreateCustomError(NotFoundErrorRedirect),
                _forbiddenError.CreateCustomError(ForbiddenErrorRedirect),
                _serverError.CreateCustomError(ServerErrorRedirect));
        }
コード例 #6
0
    private Boolean CustomErrorTest()
    {
        Boolean isError = false;

        try {
            Configuration       config  = WebConfigurationManager.OpenWebConfiguration("~/web.config");
            CustomErrorsSection section = (CustomErrorsSection)config.GetSection("system.web/customErrors");
            CustomErrorsMode    mode    = section.Mode;

            hDefaultRedirect.Text        = section.DefaultRedirect;
            hDefaultRedirect.NavigateUrl = section.DefaultRedirect;
            LCustomErrorsMode.Text       = mode.ToString();

            RCustomErrorCodes.DataSource = section.Errors;
            RCustomErrorCodes.DataBind();

            if (mode == CustomErrorsMode.Off)
            {
                isError = true;
            }
        } catch (Exception ex) {
            isError = true;
            log.Error("Error checking custom error settings in web.config.", ex);
        }

        if (isError)
        {
            LCustomErrorStatus.Text     = "Failed: Custom Errors are disabled in web.config, <br/>users may see unfriendly warnings or code details";
            LCustomErrorStatus.CssClass = CSS_STATUS_ERROR;
            LCustomErrorsMode.CssClass  = CSS_STATUS_WARN;
        }
        else
        {
            LCustomErrorStatus.Text     = "Passed";
            LCustomErrorStatus.CssClass = CSS_STATUS_NORMAL;
            LCustomErrorsMode.CssClass  = CSS_PASS;
        }

        return(isError);
    }
コード例 #7
0
        public static void Main()
        {
            // <Snippet1>

            // Get the Web application configuration.
            System.Configuration.Configuration configuration =
                WebConfigurationManager.OpenWebConfiguration(
                    "/aspnetTest");

            // Get the section.
            CustomErrorsSection customErrorsSection =
                (CustomErrorsSection)configuration.GetSection(
                    "system.web/customErrors");

            // Get the collection
            CustomErrorCollection customErrorsCollection =
                customErrorsSection.Errors;

            // </Snippet1>


            // <Snippet2>
            // Create a new CustomErrorCollection object.
            CustomErrorCollection newcustomErrorCollection =
                new System.Web.Configuration.CustomErrorCollection();

            // </Snippet2>


            // <Snippet3>
            // Get the currentDefaultRedirect
            string currentDefaultRedirect =
                customErrorsSection.DefaultRedirect;

            // </Snippet3>

            // <Snippet4>
            // Using the Set method.
            CustomError newCustomError =
                new CustomError(404, "customerror404.htm");

            // Update the configuration file.
            if (!customErrorsSection.SectionInformation.IsLocked)
            {
                // Add the new custom error to the collection.
                customErrorsCollection.Set(newCustomError);
                configuration.Save();
            }

            // </Snippet4>

            // <Snippet5>
            // Using the Add method.
            CustomError newCustomError2 =
                new CustomError(404, "customerror404.htm");

            // Update the configuration file.
            if (!customErrorsSection.SectionInformation.IsLocked)
            {
                // Add the new custom error to the collection.
                customErrorsCollection.Add(newCustomError2);
                configuration.Save();
            }

            // </Snippet5>


            // <Snippet6>
            // Using the Clear method.
            if (!customErrorsSection.SectionInformation.IsLocked)
            {
                // Execute the Clear method.
                customErrorsCollection.Clear();
                configuration.Save();
            }

            // </Snippet6>

            // <Snippet7>
            // Using the Remove method.
            if (!customErrorsSection.SectionInformation.IsLocked)
            {
                // Remove the error with statuscode 404.
                customErrorsCollection.Remove("404");
                configuration.Save();
            }

            // </Snippet7>


            // <Snippet8>
            // Using method RemoveAt.
            if (!customErrorsSection.SectionInformation.IsLocked)
            {
                // Remove the error at 0 index
                customErrorsCollection.RemoveAt(0);
                configuration.Save();
            }

            // </Snippet8>


            // <Snippet9>
            // Get the current Mode.
            CustomErrorsMode currentMode =
                customErrorsSection.Mode;

            // Set the current Mode.
            customErrorsSection.Mode =
                CustomErrorsMode.RemoteOnly;

            // </Snippet9>

            // <Snippet10>
            // Get the error with collection index 0.
            CustomError customError =
                customErrorsCollection[0];

            // </Snippet10>

            // <Snippet11>
            // Get the error with status code 404.
            CustomError customError1 =
                customErrorsCollection["404"];

            // </Snippet11>

            // <Snippet12>
            // Create a new CustomErrorsSection object.
            CustomErrorsSection newcustomErrorsSection =
                new CustomErrorsSection();

            // </Snippet12>
        }
コード例 #8
0
        //
        // default constructor when no custom errors found in config
        //

        private CustomErrors()
        {
            _mode = CustomErrorsMode.RemoteOnly;
        }
コード例 #9
0
        //
        // constructor used by config section handler
        //
        internal CustomErrors(XmlNode node, String basePath, CustomErrors parent)
        {
            _mode = CustomErrorsMode.Off;

            // inherit parent settings

            if (parent != null)
            {
                _mode            = parent._mode;
                _defaultRedirect = parent._defaultRedirect;

                if (parent._codeRedirects != null)
                {
                    _codeRedirects = new Hashtable();
                    for (IDictionaryEnumerator e = parent._codeRedirects.GetEnumerator(); e.MoveNext();)
                    {
                        _codeRedirects.Add(e.Key, e.Value);
                    }
                }
            }

            // add current settings

            XmlNode a;
            String  redirect = null;

            // get default and mode from the main tag

            HandlerBase.GetAndRemoveStringAttribute(node, "defaultRedirect", ref redirect);

            if (redirect != null)
            {
                _defaultRedirect = GetAbsoluteRedirect(redirect, basePath);
            }

            int iMode = 0;

            a = HandlerBase.GetAndRemoveEnumAttribute(node, "mode", typeof(CustomErrorsMode), ref iMode);
            if (a != null)
            {
                _mode = (CustomErrorsMode)iMode;
            }

            // report errors on bad attribures
            HandlerBase.CheckForUnrecognizedAttributes(node);

            // child tags

            foreach (XmlNode child in node.ChildNodes)
            {
                if (HandlerBase.IsIgnorableAlsoCheckForNonElement(child))
                {
                    continue;
                }

                int status = 0; // set when req. attr. is read

                // only <error> is allowed

                if (child.Name != "error")
                {
                    HandlerBase.ThrowUnrecognizedElement(child);
                }

                // status code attribure

                a = HandlerBase.GetAndRemoveRequiredIntegerAttribute(child, "statusCode", ref status);
                if (status < 100 && status > 999)
                {
                    throw new ConfigurationException(
                              HttpRuntime.FormatResourceString(SR.Customerrors_invalid_statuscode),
                              a);
                }

                // redirect attribure
                redirect = HandlerBase.RemoveRequiredAttribute(child, "redirect");

                // errors on other attributes
                HandlerBase.CheckForUnrecognizedAttributes(child);
                // <error> tags contain no content
                HandlerBase.CheckForChildNodes(child);

                // remember

                if (_codeRedirects == null)
                {
                    _codeRedirects = new Hashtable();
                }

                _codeRedirects[status.ToString()] = GetAbsoluteRedirect(redirect, basePath);
            }
        }
コード例 #10
0
        private static void LoadCustomErrorsMode()
        {
            // ~ value open default web.config in current web application
            var config = WebConfigurationManager.OpenWebConfiguration("~/");
            var section = (CustomErrorsSection)config.GetSection("system.web/customErrors");
            ErrorsMode = section.Mode;

            if (_useCustomErrorsDefinedInConfig)
            {
                StatusCodesToIgnoreFromWebConfig.Clear();

                foreach (CustomError error in section.Errors)
                {
                    //load the error code
                    int statusCode = error.StatusCode;
                    StatusCodesToIgnoreFromWebConfig.Add(statusCode);
                }
            }
        }