简体   繁体   English

ASP.NET 3.5中应用程序范围设置的好地方在哪里?

[英]Where's a good place for Application-wide settings in ASP.NET 3.5?

I have a ASP.NET Web Site/Application, and was wondering where a good place to put some application wide settings would be. 我有一个ASP.NET网站/应用程序,想知道在哪里可以进行一些应用程序范围的设置。 If feasible, I'd like to avoid rolling my own solution, and use what's already there. 如果可行,我希望避免使用自己的解决方案,而使用现有的解决方案。

Put them inside the web.config file 将它们放在web.config文件中

  <appSettings>
    <add key="MySetting" value="Value" />
  </appSettings>

and access them using ConfigurationManager. 并使用ConfigurationManager访问它们。

var value = ConfigurationManager.AppSettings["MySetting"];

Note you will have to add a reference to System.Configuration and add a using statement (assuming c#); 注意,您将必须添加对System.Configuration的引用并添加using语句(假设c#);

using System.Configuration;

Your best bet is probably in the web.config file. 最好的选择可能是在web.config文件中。 You can create an AppSettings element that can contain custom settings. 您可以创建一个可以包含自定义设置的AppSettings元素。 See http://msdn.microsoft.com/en-us/library/ms228154.aspx for more details. 有关更多详细信息,请参见http://msdn.microsoft.com/zh-cn/library/ms228154.aspx

You would then retrieve these using: 然后,您将使用以下方法检索这些:

NameValueCollection appSettings = ConfigurationManager.AppSettings;

如果您的配置比简单的键/值对更复杂,我更喜欢使用自己的配置部分

This depends on the kind of settings you want to store. 这取决于要存储的设置类型。 Most of the situations are handled well using web.config file (which is located on the root of your web application folder). 使用web.config文件(位于Web应用程序文件夹的根目录)可以很好地处理大多数情况。 A common use of it is to store connection strings to databases or a folder path. 它的常见用法是将连接字符串存储到数据库或文件夹路径。

Although you can edit it manually (it's an xml file) I advice you use the integrated UI that ASP.NET has. 尽管您可以手动编辑它(它是一个xml文件),但我建议您使用ASP.NET具有的集成UI。 In Solution Explorer select your web project, on the top of the frame you'll see a globe with a red hammer icon. 在解决方案资源管理器中,选择您的Web项目,在框架的顶部,您将看到带有红色锤子图标的地球仪。 This will launch ASP.NET Configuration which let's you edit your settings stored in web.config 这将启动ASP.NET配置,让您编辑存储在web.config中的设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM