ASP.NET 2.0:Httpcontext.current.session.add() 中的问题
任何人都可以帮我找到以下问题的解决方案。
在 ASP.NET 网站中:在 Application_OnPostAuthenticate() 事件中,我编写的任何代码都会针对每个请求执行。 因此,由于这个customidentity对象,每次请求都会调用countryid和weatherid(调用数据库获取值)。 它会影响页面的响应时间和不必要的代码执行。
void Application_OnPostAuthenticateRequest(对象发送者,EventArgs e) {
//获取当前User的引用 IPrincipal objIPrincipal = HttpContext.Current.User; // 如果我们正在处理经过身份验证的表单身份验证请求 if ((objIPrincipal.Identity.IsAuthenticated) && (objIPrincipal.Identity.AuthenticationType == "表单")) { CustomPrincipal objCustomPrincipal = new CustomPrincipal(); objCustomPrincipal = objCustomPrincipal.GetCustomPrincipalObject(objIPrincipal.Identity.Name); HttpContext.Current.User = objCustomPrincipal; CustomIdentity ci = (CustomIdentity)objCustomPrincipal.Identity; HttpContext.Current.Cache["CountryID"] = FatchMasterInfo.GetCountryID(ci.CultureId); HttpContext.Current.Cache["WeatherLocationID"] = FatchMasterInfo.GetWeatherLocationId(ci.UserId); Thread.CurrentPrincipal = objCustomPrincipal; }
}
为了解决这个问题,当我尝试按如下方式更改代码时 HttpContext.Current.Session.Add("测试", FatchMasterInfo.GetWeatherLocationId(ci.UserId);); 在缓存的地方我发现了愚蠢的错误 “对象引用未设置为对象的实例”
我不知道我们是否可以将会话变量存储在Application_OnPostAuthenticate()事件中?
Can anybody help me to find out solution of following problem.
In ASP.NET website: at Application_OnPostAuthenticate() event, whatever code i write is executed for every request. therefore due to this customidentity object, countryid and weatherid is called everytime for each request (call for database for value). It effect response time of page and unneccessary code execute.
void Application_OnPostAuthenticateRequest(object sender, EventArgs e)
{// Get a reference to the current User IPrincipal objIPrincipal = HttpContext.Current.User; // If we are dealing with an authenticated forms authentication request if ((objIPrincipal.Identity.IsAuthenticated) && (objIPrincipal.Identity.AuthenticationType == "Forms")) { CustomPrincipal objCustomPrincipal = new CustomPrincipal(); objCustomPrincipal = objCustomPrincipal.GetCustomPrincipalObject(objIPrincipal.Identity.Name); HttpContext.Current.User = objCustomPrincipal; CustomIdentity ci = (CustomIdentity)objCustomPrincipal.Identity; HttpContext.Current.Cache["CountryID"] = FatchMasterInfo.GetCountryID(ci.CultureId); HttpContext.Current.Cache["WeatherLocationID"] = FatchMasterInfo.GetWeatherLocationId(ci.UserId); Thread.CurrentPrincipal = objCustomPrincipal; }
}
To solve this problem when i try tochange code as follows
HttpContext.Current.Session.Add("test", FatchMasterInfo.GetWeatherLocationId(ci.UserId);); in place of cache i found foolowing error
"Object refrence not set to the instance of object"
I don't know whether we can store session variable inside Application_OnPostAuthenticate() event or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以稍后在请求中尝试执行此操作,例如在
PreRequestHandlerExecute
事件中:You could try doing this a bit later in the request, such as in the
PreRequestHandlerExecute
event:您可能不想在每个请求中发生的任何事件中访问会话。 有些请求甚至没有会话(例如,大量 Web 服务调用,或调用加载静态资源的 WebResource.axd)。
You probably don't want to access the session in any event that happens in every request. Some requests don't even have session (for instance, a lot of web service calls, or calls to WebResource.axd that load static resources).
在向缓存对象添加值之前,请检查该对象是否已存在于缓存中。
Before adding value to cache object, check if it already exists in the cache.
您可能没有启用会话状态。 它可以在其他地方工作吗(比如在网络表单的显示中)?
在 web.config 中的 system.web 元素下查找
元素,确保其已打开(除非您有网络场,否则将其设置为 InProc)。You might not have session state enabled. Does it work anywhere else (like in a web form's display)?
Look for a
<sessionState>
element under your system.web element in web.config make sure it's turned on (set it to InProc unless you have a web farm).