Tôi làm cách nào để truy cập biến phiên từ HttpModule
?Biến phiên truy cập từ HTTPModule
tôi thiết lập sau biến session trong trang cs, mà tôi muốn truy cập trong HttpModule
:
Session [ "username"] = "BLAH"
Tôi làm cách nào để truy cập biến phiên từ HttpModule
?Biến phiên truy cập từ HTTPModule
tôi thiết lập sau biến session trong trang cs, mà tôi muốn truy cập trong HttpModule
:
Session [ "username"] = "BLAH"
Dưới đây là một ví dụ của việc sử dụng phiên trong HttpModule, được tìm thấy 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; }
}
}
}
Tôi đề xuất phương pháp ít xâm lấn hơn. Chỉ cần lực lượng ASP.NET runtime để hiển thị phiên cho mô-đun của bạn với SetSessionStateBehavior() (việc triển khai IRequiresSessionState chỉ được xem xét cho 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);
}
'OriginalHandler.ProcessRequest (context); 'trong' ProcessRequest' nếu mã của bạn ** thực sự không ** gọi' ProcessRequest () ' –
TheGeekZn
Bất cứ ai cũng biết nếu điều này vẫn còn hiệu lực? Tôi nhận được lỗi trong debug.assert nói rằng phiên không có sẵn trong ngữ cảnh này. – Martin