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