代码隐藏C#的会话超时 [英] Session timeout from codebehind C#

查看:124
本文介绍了代码隐藏C#的会话超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我有一个使用asp.net 4.0(C#)创建的web应用程序。



应用程序的会话超时是默认的(20分钟),但是对于只有一种形式我需要将会话超时增加到2小时



问候,

Varun Vimal



我尝试过:



i试图添加HttpSessionState.Timeout = 10;(10分钟用于测试目的,但它给出了一个错误):



非对象引用是必需的-static字段,方法或属性'HttpSessionState.Timeout'

解决方案

找到了灵魂



 Session.Timeout =  10 ; 


首先,不应该扩展会话 - 它会占用服务器资源,并且可以减慢爬行速度。这是一些托管服务从默认的20分钟减少它,并忽略扩展它的请求!



如果你得到对象参考是必需的......该行的错误,然后你需要查看变量HttpSessionState - 它的值是null,我不确定你从哪里得到它...



通常,会话超时将在web.config文件中设置,而不是在代码中设置: HttpSessionState.Timeout属性(System.Web.SessionState) [ ^ ]但严重的是,如果你需要更长的持久性,你可能应该是看起来不依赖于会话,而是使用DB和cookie的组合来存储。扩展会话不是一个好主意。


从这些链接中查看答案:



c# - 非静态字段需要对象引用,方法或Windows窗体上的属性 - Stack Overflow [ ^ ]



C# error:非对象引用是必需的静态字段,方法或属性 - 堆栈溢出 [ ^ ]



会话状态| ASP.NET论坛 [ ^

hello,

I have a web application created using asp.net 4.0(C#).

The session timeout for the application is default(20 mins), but for only one form i need to increase the session timeout to 2 hrs

Regards,
Varun Vimal

What I have tried:

i tried adding HttpSessionState.Timeout = 10;(10 mins for testing purpose but it gave an error) :

An object reference is required for the non-static field, method, or property 'HttpSessionState.Timeout'

解决方案

found the soultion

Session.Timeout = 10;


Firstly, the Session shouldn't be extended - it ties up server resources, and can slow things to a crawl. That's way some hosting services reduce it from the default 20 minutes, and ignore requests to extend it!

If you are getting an "object reference is required..." error for that line, then you need to look at the variable HttpSessionState - it's value is null, and I'm not at all sure where you got it from...

Normally, the session timeout would be set in the web.config file, rather than in code:
HttpSessionState.Timeout Property (System.Web.SessionState)[^] but seriously, if you need longer persistance, you should probably be lookign to not rely on teh session byut use a combination of DB and cookies for storage instead. Extending the session is not a good idea.


See the answers from these links :

c# - An object reference is required for the nonstatic field, method, or property on a Windows form - Stack Overflow[^]

C# error : An object reference is required for the non-static field, method, or property - Stack Overflow[^]

Session State | The ASP.NET Forums[^]


这篇关于代码隐藏C#的会话超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆