简体   繁体   English

在ASP.NET 2.0中管理设置(配置)的最佳方法是什么?

[英]What is the best way to manage settings (configuration) in ASP.NET 2.0?

I have a project for which we are extending some functionality from an existing client into a web portal in ASP.NET 2.0. 我有一个项目,我们将其功能从现有客户端扩展到ASP.NET 2.0中的Web门户。 The client is windows forms based (in .NET 2.0). 客户端基于Windows窗体(在.NET 2.0中)。 It has some settings that are kept in the Project > Properties > Settings.settings system, including a generated settings.Designer.cs file. 它具有一些设置,这些设置保留在“项目”>“属性”>“ Settings.settings”系统中,包括生成的settings.Designer.cs文件。 This file provides nice automatic wrappers for each of the settings. 该文件为每个设置提供了不错的自动包装器。

In trying to set up the website I have been frustrated by an apparent lack of parity for this feature. 在尝试建立网站时,我对于此功能的明显缺乏同等性感到沮丧。 I have a web.config, and it can have an section. 我有一个web.config,它可以有一个部分。 This means access via code with strings, for example: 这意味着通过带有字符串的代码进行访问,例如:

WebConfigurationManager.AppSettings["mySetting"];

I can even have the settings refer to another file this way, affording a little abstraction, and easier check-ins to source control: 我什至可以使设置以这种方式引用另一个文件,从而提供一点抽象,并更易于签入到源代码管理中:

<appSettings configSource="web.settings.config"/>

but ultimately this lacks some of the functionality of the client projects settings system. 但是最终,这缺少客户端项目设置系统的某些功能。

Particularly I would very much like these features if at all possible: 特别是如果可能的话,我非常喜欢这些功能:

  • Autogenerated accessor class (for convenience, intellisense..) 自动生成的访问器类(为方便起见,智能化..)
    • Convenient 方便
    • Strongly Typed 强类型
    • Provides Intellisense 提供智能感知
    • Code will not compile against typos/mistakes in settings names 代码不会针对设置名称中的错别字/错误进行编译
  • Easy Interface 简易介面
    • The settings.Settings before provided a nice grid 设置。之前的设置提供了一个不错的网格
    • All options represented 代表所有选项
    • Showed dropdown choices for certain options 显示某些选项的下拉选项
    • Don't have to edit XML 不必编辑XML
    • Can't fat-finger an angle bracket 不能用胖手指指尖括号

I know that it would be possible to create a class that wrapped these, but it would not stay in sync with the settings automatically, and would have to be manually edited on any change. 我知道可以创建一个包装这些内容的类,但是它不会自动与设置保持同步,因此必须对任何更改进行手动编辑。 It would provide some of the above however. 但是,它将提供一些上述内容。

Can I get parity with the project settings like we do in our client? 我可以像在客户中一样获得项目设置的优势吗?

What is the best way to manage settings for an ASP.NET 2.0 website? 管理ASP.NET 2.0网站设置的最佳方法是什么?

您应该使用Web应用程序,而不是网站,并且可以使用与Windows应用程序相同的方式来访问设置

By default, using the appSettings section in your web.config is the simplest way to manage your application settings for the web application. 默认情况下,使用web.config中的appSettings部分是管理Web应用程序的应用程序设置的最简单方法。 You can cast the values within the application settings as needed. 您可以根据需要在应用程序设置中强制转换值。 However, you will lose strong typing in this case, which sounds like a deal breaker to you. 但是,在这种情况下,您将失去强类型输入功能,这听起来像是一笔破坏交易的事情。

If you do need to retain the strong typing, go into the project settings (similar to the WinForms application) and set your values there. 如果确实需要保留强类型,请进入项目设置(类似于WinForms应用程序)并在那里设置值。 You'll notice that settings.settings files exists within a MyProjects folder, but you'll also find the following code snippet down at the bottom of your web.config file (swiped from one of my applications): 您会注意到,settings.settings文件存在于MyProjects文件夹中,但是您还会在web.config文件的底部找到了以下代码片段(从我的一个应用程序中清除):

<applicationSettings>
   <EAF.My.MySettings>
      <setting name="EAF_AS400EmployeeServices_EmployeeServices" serializeAs="String">                                          
      <value>http://countynetappsdev/AS400DataService/EmployeeServices.asmx</value>
  </setting>
</applicationSettings>

Notice the serializeAs property in this section. 注意本节中的serializeAs属性。 Once you have things in this section, you can access the settings through the My.Settings library using VB.NET. 在本节中有了内容之后,就可以使用VB.NET通过My.Settings库访问设置。 However, since you're using C#, you need a smidge more work, but a simple code solution can be found here . 但是,由于您正在使用C#,因此需要做更多的工作,但是可以在此处找到一个简单的代码解决方案。

I create classes that wrap my web.config to provide strongly typed, intellisense friendly access to the settings. 我创建了包装web.config的类,以提供对类型的强类型,智能感知的访问。

Also, the settings are most likely to be changed in the XML by the support guys at implementation time, so the grid in VS makes no difference. 另外,支持人员很可能在实现时更改XML中的设置,因此VS中的网格没有任何区别。 Granted, its not auto generated, but copy/paste is your friend :) 当然,它不是自动生成的,但是复制/粘贴是您的朋友:)

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

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