【问题标题】:Accessing SessionState in IHttpModule after 404?在 404 之后访问 IHttpModule 中的 SessionState?
【发布时间】:2009-05-15 16:05:49
【问题描述】:

是否可以在 404 之后的 HttpModule 的错误事件处理程序中访问 SessionState?

我正在尝试使用此博客文章中描述的技术为完整和部分回发实现一致的错误处理机制,

ASP.NET Error Handling......

我试图将异常推送到会话状态并从错误页面访问它,而不是在查询字符串上传递大量参数。

在我执行 Server.Transfer(在 HttpModule 的错误处理程序中)时,SessionState 永远不可用,因此对错误页面不可用。

我尝试了将 IHttpHandler 重置为具有 IRequestSessionState 接口的技巧,但没有任何乐趣。

提前致谢,

编辑 - IHttpModule 错误处理程序的代码是,

void context_Error(object sender, EventArgs e)
{
    try
    {            
        var srcPageUrl = HttpContext.Current.Request.Url.ToString();                       

        // If the error comes our handler we retrieve the query string
        if (srcPageUrl.Contains(NO_PAGE_STR))
        {
            // Deliberate 404 from page error handler so transfer to error page
            // SESSION NOT AVAILABLE !!!!

            HttpContext.Current.ClearError();                
            HttpContext.Current.Server.Transfer(string.Format("{0}?ErrorKey={1}", ERROR_PAGE_URL, errorKey), true);
        }
        else            
            HttpContext.Current.Server.ClearError();                

        return;
    }
    catch (Exception ex)
    {
        Logging.LogEvent(ex);
    }
} 

马特

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:
        public class ExceptionModule : IHttpModule
        {
            #region IHttpModule Members
    
            public void Dispose()
            {
    
            }
    
        /// <summary>
        /// Init method to register the event handlers for 
        /// the HttpApplication
        /// </summary>
        /// <param name="application">Http Application object</param>
        public void Init(HttpApplication application)
        {
            application.Error += this.Application_Error;
        }
    
    
    
    
        private void Application_Error(object sender, EventArgs e)
        {
    
                        // Create HttpApplication and HttpContext objects to access
                        // request and response properties.
                        var application = (HttpApplication)sender;
                        var context = application.Context;
                        application.Session["errorData"] = "yes there is an error";
    
                        context.Server.Transfer("Error.aspx");
    
             }
    }
    

    这应该可以解决问题!为我工作!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      • 1970-01-01
      • 2010-10-30
      相关资源
      最近更新 更多