从应用程序内更改 Web.Config SessionTimeout 值
我正在尝试找出一种动态改变会话超时的方法,因为我的客户要求这是他们可以轻松更改的值(就好像它是数据库事务一样)。这是我试图更改的值的截断版本:
<appSettings>
<add key="SessionTimeout" value="20"/>
</appsettings>
我正在使用 C#/.Net 3.5 处理所有这些。
根据记录,我知道更改 web.config 文件将导致应用程序重新启动。我想要的是让程序管理员能够动态更改超时。例如,如果他们要求人们填写的其中一份表格花费的时间超过 20 分钟,那么他们需要将时间增加到半小时,或者如果他们出于其他原因希望缩短时间。由于我们希望让用户(包括管理员)远离代码文件,因此目标是将其创建为网站管理页面的一部分。
问题是,我不知道如何做到这一点,或者是否可能(更不用说一个好主意)。我已经用谷歌搜索了一圈,但一无所获。
有没有办法进行这些更改,甚至有可能动态更改超时(覆盖 web.config 的内容)?
感谢您的帮助。
I am trying to work out a way to alter the session timeout dynamically, as my customer has requested that this be a value they can easily change (as though it were a database transaction). Here is a truncated version of the value I am trying to change:
<appSettings>
<add key="SessionTimeout" value="20"/>
</appsettings>
I'm working with C#/.Net 3.5 with all of this.
For the record, I am aware that altering the web.config file will cause a restart of the application. What I am going for is to give the administrator of the program the ability to alter the timeout on the fly. If, for example, one of the forms they are requiring people to fill out is taking longer than 20 minutes, and they need to bump the time up to a half hour, or if they want it shortened for some other reason. Since we want to keep the users (including admins) out of the code files, the goal is to create this as part of the site's admin page.
The problem is, I have no idea how to do this, or if it's even possible (much less a good idea). I've given google the run around and come up with nothing.
Is there a way to make these alterations, or even potentially to dynamically change the timeout on the fly (overriding the contents of the web.config)?
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在运行时编辑 web.config 是可能的,但正如您已经指出的,我不确定这是否是一个好主意。有多种方法可以实际编辑此文件,但是您必须确保运行应用程序的帐户具有适当的文件系统权限,以便能够写入应用程序目录(例如编辑和保存 web.config 文件)。如果您处于集群环境中,我强烈建议不要走这条路(尽管仍然有可能)。
一旦你的权限等被整理好,下面是一个快速的方法。
我希望这会有所帮助,但我认为您应该选择一个较大的会话时间,这样会更容易,并且在将来造成的问题也更少。
Editing your web.config at runtime is possible, but as you have already pointed out, I'm not sure if it is a good idea. There are multiple ways you can go about actually doing the editing of this file, however you have to make sure that the account you are running your application under has the appropriate file system permissions to be able to write to the application directory (eg edit and save the web.config file). If you are in a clustered environment, I would strongly advise not going down this path (though still possible).
Once your permissions etc have been sorted out, below is a quick way of doing it.
I hope this helps, but I think you should just pick a large session time would be easier and cause less problems in the future.
与其尝试更改 web.config,不如在运行时更改代码中的 Session.Timeout 怎么样?
更多信息:
http://msdn.microsoft。 com/en-us/library/system.web.sessionstate.httpsessionstate.timeout.aspx
Instead of trying to change web.config, how about changing Session.Timeout in code at runtime ?
More info :
http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.timeout.aspx
在需要的地方延长代码超时:
此外,如果您尝试在配置中更改此参数,则会导致应用程序池重新启动。
Extend timeout from code where you need it:
Also if you will try to change this parameter in config it will lead to application pool restart.