如何使用 ASP.NET 在 web.config 文件中写入 appSetting?
我可以轻松地读取 web.config 文件中的 appSetting,然后在 ASP.NET 表单中填写所有键和值。但我需要对我的网络应用程序进行初始设置,如何在 appSetting 中编写? 这是最好的方法还是我必须将所有密钥存储在数据库中哪个更好? 我怎样才能实现这个?我需要添加一个键并在 appSetting 配置文件中编辑键或其值。
I can easily read my appSetting in web.config file and then fill all keys and values in asp.net form. but I need to have initial setup for my web application how I can write in appSetting ?
Is it the best way or not I have to store all of the keys in database which is better ?
and How I can implement this ? I need to add a key and edit a key or their value in appSetting config file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不建议操作 web.config 文件中的值,因为一旦用户尝试修改值,所有用户
会话都将被终止
。当
Machine.Config、Web.Config 或 Global.asax
中发生任何更改时,应用程序域将会卸载。阅读 ASP.NET 案例研究:丢失会话变量和应用程序域回收
如果您的密钥是可修改的,那么最好将其存储在数据库中。
Manipulating values in the web.config
file is not recommend, because once a user tries to modify a value, all Usersessions will be terminated
.An application domain will unload when any change occurs in the
Machine.Config, Web.Config, or Global.asax
.Read ASP.NET Case Study: Lost session variables and appdomain recycles
If your keys are modifiable, then it is preferable to store it in DB.
如何创建初始设置文件?
您可以在 Visual Studio 中执行此操作,手动编辑 web.config 文件或使用 Visual Studio 的设置 UI。
使用数据库或配置文件进行设置?
这取决于您的要求。如果您有多个应用程序实例并且希望它们共享相同的设置,您可能希望在数据库中拥有设置。这样,当您在一个实例中更新设置而另一个实例继续使用旧设置时,您将不会遇到同步问题和细微错误。
如果您经常更新设置,那么数据库设置可能也是一个更好的主意,因为 web.config 中的更改会触发应用程序重新启动。如果您只有一个实例并且很少更改设置,那么使用 web.config 文件会更容易。根据要求,您最终可能会混合使用这两种方法。
如何实现DB设置?
您可以拥有包含两个字符串列名称/值的表格,也可以拥有包含一行和与您拥有的设置一样多的列的表格。第二种方法是更“类型化”的方法,因为您可以分别为每列指定适当的数据类型。
How to create initial settings file?
You can do it in Visual Studio, either manually editing web.config file or using Settings UI of Visual Studio.
To use DB or config file for settings?
It depends on your requirements. If you have more than on instance of application and you want them to share the same settings you might want to have settings in DB. This way you will not have sync issues and subtle bugs when you update settings in one instance and another keeps using old settings.
If you update settings often, then DB settings might be a better idea too, because changes in web.config trigger application restart. If you have one instance and rarely change settings, it is easier to go with web.config file. Depending on the requirements you might end up with the mix of those two approaches.
How to implement DB settings?
You can have table with two string columns Name/Value, or you can have table with one row and as many columns as many settings you have. The second approach is more 'typed' approach as you can specify appropriate data type for each column separately.
这取决于应用程序有多大。
如果您计划了一个丰富的
后端
,那么首选使用数据库作为配置存储。当然,这种方法还需要做更多的工作。如果有一些简单的配置并且网站管理员需要手动更改它们,则首选 web.config,因为当所有事情都应该手动完成时,更改数据库并不那么容易。
不必担心修改 web.config 和丢失会话。实际上,在应用程序负载最小时维护几分钟是可以接受的方法(您经常在 stackoverflow 和其他著名的页面上看到维护页面)
It depends how big is the application.
If you have planned a rich
back-end
then using database as configurations storage is preferred. Of course this approach needs more work.If there's some simple configurations and webmaster should change them manually, web.config is preferred since changing db is not so easy when all things should be done manually.
Don't bother about modifying web.config and loosing sessions. In practice, maintenance for a few minutes when application load is minimum is an acceptable approach (you see maintenance page frequently on stackoverflow and other famous ones)