2010-10-26 70 views
3

我有一個我寫過的需要訪問會話的HTTP模塊。我也做了以下內容:未在擴展名頁面中設置HTTP模塊會話

  • 模塊是在web.config中註冊
  • 模塊重視我的方法調用PostAcquireRequestState事件
  • 模塊實現IRequiresSessionState

然而,當我的網頁沒有按沒有擴展名(例如htp://www.mywebsite.com)會話不可用,代碼失敗。如果頁面確實有一個aspx擴展名,那麼一切正常。

+0

如何訪問會話?你可以添加(你的http模塊代碼的簡化版本)嗎? – 2010-10-26 13:16:30

回答

0

爲了使您的模塊成爲請求生命週期的一部分,您需要有一個由ASP.NET處理的項目。像index.html這樣的服務頁面將無法實現。一個ASPX頁面將會。

+0

雖然我做。由根加載的頁面是default.aspx。如果我特別請求default.aspx然後會話可用,但是如果我只使用http://www.mywebsite.com那麼它不是? – januszstabik 2010-10-26 14:12:15

+0

頁面是否可能被瀏覽器緩存? – Pedro 2010-10-26 14:14:53

+0

不,我可以逐步完成請求並在每一步評估請求。 – januszstabik 2010-10-26 15:48:17

0

從以下線程的代碼做的伎倆(1):

public class Module : IHttpModule, IRequiresSessionState 
{ 
    public void Dispose() 
    { 
    } 

    void OnPostMapRequestHandler(object source, EventArgs e) 
    { 
     HttpApplication app = (HttpApplication)source; 

     if (app.Context.Handler is IReadOnlySessionState || app.Context.Handler is IRequiresSessionState) 
      return; 

     app.Context.Handler = new MyHttpHandler(app.Context.Handler); 
    } 

    void OnPostAcquireRequestState(object source, EventArgs e) 
    { 
     HttpApplication app = (HttpApplication)source; 

     MyHttpHandler resourceHttpHandler = HttpContext.Current.Handler as MyHttpHandler; 

     if (resourceHttpHandler != null) 
      HttpContext.Current.Handler = resourceHttpHandler.OriginalHandler; 
    } 

    public void Init(HttpApplication httpApp) 
    { 
     httpApp.PostAcquireRequestState += new EventHandler(OnPostAcquireRequestState); 
     httpApp.PostMapRequestHandler += new EventHandler(OnPostMapRequestHandler); 
    } 

    public class MyHttpHandler : IHttpHandler, IRequiresSessionState 
    { 
     internal readonly IHttpHandler OriginalHandler; 

     public void ProcessRequest(HttpContext context) 
     { 
      throw new InvalidOperationException("MyHttpHandler cannot process requests."); 
     } 

     public MyHttpHandler(IHttpHandler originalHandler) 
     { 
      OriginalHandler = originalHandler; 
     } 

     public bool IsReusable 
     { 
      get { return false; } 
     } 
    } 
} 
+0

你好,不幸的是這不起作用。我已經嘗試過,但無濟於事。即使在交換處理程序之後,請求擴展頁面時也沒有可用的會話狀態。還有什麼可能導致這個?我在已經安裝了幾個http模塊的Umrbaco源代碼上運行它,但是,它們中沒有一個使用會話? – januszstabik 2010-10-27 08:07:05

+0

你在看哪個網頁服務器? – Nariman 2010-10-27 11:32:45