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. 访问会话变量8
- 2. UrlRewriter +的HttpModule +会话问题
- 3. 访问来自socket的快速会话
- 4. 类中的访问会话变量
- 5. 来自URL的PHP会话变量
- 6. 来自SQL DB的Asp.net会话变量
- 7. 来自DLL的会话变量值
- 8. RSpec spec_helper访问会话变量
- 9. 无法调用访问会话变量
- 10. Spring MVC 3.0访问会话变量
- 11. ASP.NET MVC3会话变量不可访问
- 12. 使用会话变量访问页面
- 13. PHP会话变量不可访问
- 14. Spring框架访问会话变量
- 15. ASP.NET:访问会话变量global.asax
- 16. 从JavaScript访问ASP.NET会话变量
- 17. 如何在express.js中访问来自express和节点js的会话变量
- 18. 来自父swf的as3访问变量
- 19. 无法在cookie上访问会话变量减少PHP会话
- 20. 会话变量访问不带会话娱乐
- 21. 可以从@Page访问会话变量,但不能从基础类访问会话变量
- 22. 访问自定义会话
- 23. 使用HttpModule在后台服务访问会话
- 24. 会话变量问题php
- 25. PHP - 会话变量问题
- 26. 为什么HttpContext的是更好地访问会话变量比直接会话
- 27. 如何访问CakePHP中的.js文件中的会话变量
- 28. 访问使用JavaScript的PHP文件中的会话变量?
- 29. 来自不同主机的不同dabatase的会话变量
- 30. Magento的 - 访问会话自定义模块内的可变
'OriginalHandler.ProcessRequest(背景);''中如果ProcessRequest'代码**实际上并**调用'的ProcessRequest()' –
TheGeekZn
2014-08-08 09:15:01
人知道这是否仍然有效?我在debug.assert中发现错误,表示会话在此上下文中不可用。 – Martin 2016-02-03 21:50:06