从应用程序内更改 Web.Config SessionTimeout 值

发布于 2024-11-10 16:32:02 字数 534 浏览 1 评论 0原文

我正在尝试找出一种动态改变会话超时的方法,因为我的客户要求这是他们可以轻松更改的值(就好像它是数据库事务一样)。这是我试图更改的值的截断版本:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

铁轨上的流浪者 2024-11-17 16:32:02

在运行时编辑 web.config 是可能的,但正如您已经指出的,我不确定这是否是一个好主意。有多种方法可以实际编辑此文件,但是您必须确保运行应用程序的帐户具有适当的文件系统权限,以便能够写入应用程序目录(例如编辑和保存 web.config 文件)。如果您处于集群环境中,我强烈建议不要走这条路(尽管仍然有可能)。

一旦你的权限等被整理好,下面是一个快速的方法。

public void EditMyWebConfig(string appSettingsKey, string appSettingsValue, string         filePath)
    {
        XmlDocument xml = new XmlDocument();
        //Load current config into memory
        xml.Load(filePath);
        //Overwrite config
        using(FileStream fs = new FileStream(filePath, FileMode.Create,FileAccess.ReadWrite))
        {
            //select appSetting you want to edit
            XmlNode xmlN = xml.SelectSingleNode("//configuration/appSettings/add[@key = '" + appSettingsKey + "']");
            //assign new value
            xmlN.Attributes["value"].Value = appSettingsValue;
            //Create new web config
            xml.Save(fs);
        }
    }

我希望这会有所帮助,但我认为您应该选择一个较大的会话时间,这样会更容易,并且在将来造成的问题也更少。

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.

public void EditMyWebConfig(string appSettingsKey, string appSettingsValue, string         filePath)
    {
        XmlDocument xml = new XmlDocument();
        //Load current config into memory
        xml.Load(filePath);
        //Overwrite config
        using(FileStream fs = new FileStream(filePath, FileMode.Create,FileAccess.ReadWrite))
        {
            //select appSetting you want to edit
            XmlNode xmlN = xml.SelectSingleNode("//configuration/appSettings/add[@key = '" + appSettingsKey + "']");
            //assign new value
            xmlN.Attributes["value"].Value = appSettingsValue;
            //Create new web config
            xml.Save(fs);
        }
    }

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.

邮友 2024-11-17 16:32:02

与其尝试更改 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

成熟稳重的好男人 2024-11-17 16:32:02

在需要的地方延长代码超时:

Session.Timeout = 40;

此外,如果您尝试在配置中更改此参数,则会导致应用程序池重新启动。

Extend timeout from code where you need it:

Session.Timeout = 40;

Also if you will try to change this parameter in config it will lead to application pool restart.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文