5
A
回答
3
下面是使用的一個例子在HttpModule中的會話,發現here:
using System;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Diagnostics;
// This code demonstrates how to make session state available in HttpModule,
// regradless of requested resource.
// author: Tomasz Jastrzebski
public class MyHttpModule : IHttpModule
{
public void Init(HttpApplication application)
{
application.PostAcquireRequestState += new EventHandler(Application_PostAcquireRequestState);
application.PostMapRequestHandler += new EventHandler(Application_PostMapRequestHandler);
}
void Application_PostMapRequestHandler(object source, EventArgs e)
{
HttpApplication app = (HttpApplication)source;
if (app.Context.Handler is IReadOnlySessionState || app.Context.Handler is IRequiresSessionState) {
// no need to replace the current handler
return;
}
// swap the current handler
app.Context.Handler = new MyHttpHandler(app.Context.Handler);
}
void Application_PostAcquireRequestState(object source, EventArgs e)
{
HttpApplication app = (HttpApplication)source;
MyHttpHandler resourceHttpHandler = HttpContext.Current.Handler as MyHttpHandler;
if (resourceHttpHandler != null) {
// set the original handler back
HttpContext.Current.Handler = resourceHttpHandler.OriginalHandler;
}
// -> at this point session state should be available
Debug.Assert(app.Session != null, "it did not work :(");
}
public void Dispose()
{
}
// a temp handler used to force the SessionStateModule to load session state
public class MyHttpHandler : IHttpHandler, IRequiresSessionState
{
internal readonly IHttpHandler OriginalHandler;
public MyHttpHandler(IHttpHandler originalHandler)
{
OriginalHandler = originalHandler;
}
public void ProcessRequest(HttpContext context)
{
// do not worry, ProcessRequest() will not be called, but let's be safe
throw new InvalidOperationException("MyHttpHandler cannot process requests.");
}
public bool IsReusable
{
// IsReusable must be set to false since class has a member!
get { return false; }
}
}
}
3
我建議一種侵入性較小的方法。 只需強制ASP.NET運行時使用SetSessionStateBehavior()向您的模塊公開會話(IRequiresSessionState的實現僅被視爲IHttpHandlers)。
public void Init(HttpApplication httpApp)
{
//SESSION WILL BE AVAILABLE IN ALL EVENTS FROM PreRequestHandlerExecute TO PostRequestHandlerExecute
httpApp.PostRequestHandlerExecute += OnPostRequestHandlerExecute;
//THIS IS THE IMPORTANT LINE
httpApp.Context.SetSessionStateBehavior(SessionStateBehavior.ReadOnly);
}
相關問題
- 1. 訪問來自JavaScript的會話變量
- 2. 訪問會話變量8
- 3. JSTL:訪問會話變量
- 4. 訪問會話變量
- 5. UrlRewriter +的HttpModule +會話問題
- 6. 訪問來自socket的快速會話
- 7. 來自EntityRepository的訪問會話
- 8. 類中的訪問會話變量
- 9. 來自SQL DB的Asp.net會話變量
- 10. 來自URL的PHP會話變量
- 11. 來自DLL的會話變量值
- 12. 在servlet外部訪問會話變量
- 13. 從jQuery訪問會話變量
- 14. Spring框架訪問會話變量
- 15. 模型訪問會話變量?
- 16. 從JavaScript訪問ASP.NET會話變量
- 17. Spring MVC 3.0訪問會話變量
- 18. 如何訪問會話變量在Laravel
- 19. 在JavaScript中訪問會話變量
- 20. 無法調用訪問會話變量
- 21. 無法訪問會話變量
- 22. 從webroot訪問cakephp會話變量
- 23. ASP.NET MVC3會話變量不可訪問
- 24. 使用會話變量訪問頁面
- 25. 在Nginx中訪問會話變量
- 26. 從類擴展訪問會話變量
- 27. 從WebView訪問會話變量
- 28. 使用javascript訪問vbscript會話變量
- 29. PHP會話變量不可訪問
- 30. RSpec spec_helper訪問會話變量
'OriginalHandler.ProcessRequest(背景);''中如果ProcessRequest'代碼**實際上並**調用'的ProcessRequest()' –
TheGeekZn
2014-08-08 09:15:01
人知道這是否仍然有效?我在debug.assert中發現錯誤,表示會話在此上下文中不可用。 – Martin 2016-02-03 21:50:06