示例#1
0
/* obsolete
 *      private void Application_BeginRequest(Object source, EventArgs e)
 *      {
 *          HttpApplication application = (HttpApplication)source;
 *          HttpContext context = application.Context;
 *          if (context != null)
 *          {
 *              // Some device/gateway combination sends postdata's charset
 *              // in a separate header rather than in Content-Type.
 *              SetCharsetInRequestHeader(context);
 *          }
 *      }
 *
 *      private void SetCharsetInRequestHeader(HttpContext context)
 *      {
 *          String userAgent = context.Request.UserAgent;
 *
 *          if (userAgent != null && CultureInfo.InvariantCulture.CompareInfo.IsPrefix(userAgent, "UP"))
 *          {
 *              String postDataCharset = context.Request.Headers["x-up-devcap-post-charset"];
 *              if (postDataCharset != null && postDataCharset.Length > 0)
 *              {
 *                  try
 *                  {
 *                      context.Request.ContentEncoding = Encoding.GetEncoding(postDataCharset);
 *                  }
 *                  catch
 *                  {
 *                      // Exception may be thrown when charset is not valid.
 *                      // In this case, do nothing, and let the framework
 *                      // use the configured RequestEncoding setting.
 *                  }
 *              }
 *          }
 *      }
 */

/* Obsolete
 *      private void Application_EndRequest(Object source, EventArgs e)
 *      {
 *          HttpApplication application = (HttpApplication)source;
 *          HttpContext context = application.Context;
 *          if (context != null)
 *          {
 *              MobileRedirect.CheckForInvalidRedirection(context);
 *          }
 *      }
 */



        private void Application_Error(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext     context     = null;
            bool            useAdaptiveErrorReporting = false;

            try
            {
                context = application.Context;

                if (context.IsCustomErrorEnabled)
                {
                    return;
                }

                Exception error = context.Server.GetLastError();

                if ((error == null) || (!RequiresAdaptiveErrorReporting(context, error)))
                {
                    return;
                }

                useAdaptiveErrorReporting = true;

                MobileErrorInfo errorInfo = new MobileErrorInfo(error);
                context.Items[MobileErrorInfo.ContextKey] = errorInfo;

                context.Response.Clear();
                IHttpHandler errorHandler = CreateErrorFormatter(context);
                errorHandler.ProcessRequest(context);
            }
            catch (Exception e2)
            {
                if (useAdaptiveErrorReporting && context != null)
                {
                    // Failed to format error. Let it continue through
                    // default processing.

                    context.Response.Write(e2.ToString());
                    context.Server.ClearError();
                    return;
                }
                else
                {
                    return;
                }
            }

            context.Server.ClearError();
        }
        /// <include file='doc\ErrorFormatterPage.uex' path='docs/doc[@for="ErrorFormatterPage.OnInit"]/*' />
        protected override void OnInit(EventArgs e)
        {
            base.OnInit (e);

            EnableViewState = false;
            _errorInfo = Context.Items[MobileErrorInfo.ContextKey] as MobileErrorInfo;
            if (_errorInfo == null)
            {
                // Don't care what kind of exception, since it'll be handled
                // quietly by the error handler module.
                throw new Exception ();
            }

            InitContent ();
        }
/* obsolete
        private void Application_BeginRequest(Object source, EventArgs e) 
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            if (context != null)
            {
                // Some device/gateway combination sends postdata's charset
                // in a separate header rather than in Content-Type.
                SetCharsetInRequestHeader(context);
            }
        }

        private void SetCharsetInRequestHeader(HttpContext context)
        {
            String userAgent = context.Request.UserAgent;

            if (userAgent != null && CultureInfo.InvariantCulture.CompareInfo.IsPrefix(userAgent, "UP"))
            {
                String postDataCharset = context.Request.Headers["x-up-devcap-post-charset"];
                if (postDataCharset != null && postDataCharset.Length > 0)
                {
                    try
                    {
                        context.Request.ContentEncoding = Encoding.GetEncoding(postDataCharset);
                    }
                    catch
                    {
                        // Exception may be thrown when charset is not valid.
                        // In this case, do nothing, and let the framework
                        // use the configured RequestEncoding setting.
                    }
                }
            }
        }
*/

/* Obsolete
        private void Application_EndRequest(Object source, EventArgs e) 
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            if (context != null)
            {
                MobileRedirect.CheckForInvalidRedirection(context);
            }
        }
*/



        private void Application_Error(Object source, EventArgs e) 
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = null;
            bool useAdaptiveErrorReporting = false;

            try
            {
                context = application.Context;

                if(context.IsCustomErrorEnabled)
                {
                    return;
                }
    
                Exception error = context.Server.GetLastError();

                if ((error == null) || (!RequiresAdaptiveErrorReporting(context, error)))
                {
                    return;
                }

                useAdaptiveErrorReporting = true;
    
                MobileErrorInfo errorInfo = new MobileErrorInfo(error);
                context.Items[MobileErrorInfo.ContextKey] = errorInfo;
    
                context.Response.Clear();
                IHttpHandler errorHandler = CreateErrorFormatter(context);
                errorHandler.ProcessRequest(context);
            }
            catch(Exception e2)
            {
                if (useAdaptiveErrorReporting && context != null)
                {
                    // Failed to format error. Let it continue through
                    // default processing.

                    context.Response.Write(e2.ToString());
                    context.Server.ClearError();
                    return;
                }
                else
                {
                    return;
                }
            }

            context.Server.ClearError();
        }