[翻译]  Custom type application settings in ASP.NET

[CHINESE]  ASP.NET中的自定义类型应用程序设置


Just now I came across ApplicationSettings in .NET WinForms that could handle complex types.
Currently I am using AppSettings in my ASP.NET WebForms which can handle only string.
Can I use ApplicationSettings in Webforms? If so how?

刚才我遇到了可以处理复杂类型的.NET WinForms中的ApplicationSettings。目前我在我的ASP.NET WebForms中使用AppSettings,它只能处理字符串。我可以在Webforms中使用ApplicationSettings吗?如果是这样的话?

2 个解决方案

#1


37  

The basic idea:

基本理念:

  1. In a different project, create classes that will hold your custom settings. For example:

    在另一个项目中,创建将保留自定义设置的类。例如:

    public class EndPoint
    {
        public string HostName { get; set; }
        public int Port { get; set; }
    }
    
    public class EndPointCollection : Collection<EndPoint>
    {
    }
    
  2. Build the project containing the classes.

    构建包含类的项目。

  3. Go to the Settings tab in Project Properties. It will say that there is no settings file yet and ask if you want to create it.

    转到“项目属性”中的“设置”选项卡。它会说还没有设置文件,并询问您是否要创建它。

  4. Add a new settings file. In the type field select Browse and type the full class name. For example: ClassLibrary.EndPointCollection. Save and rebuild the project.

    添加新的设置文件。在类型字段中,选择“浏览”并键入完整的类名。例如:ClassLibrary.EndPointCollection。保存并重建项目。

  5. Hit the edit button for the setting value. (Note that this will not be available if the classes made in the earlier step are in the same project.) Use the UI to edit the settings.

    点击设置值的编辑按钮。 (请注意,如果在前一步中创建的类位于同一项目中,则无法使用此选项。)使用UI编辑设置。

    Visual Studio's settings value editor

  6. If you open the web.config / app.config file, you will see something like this:

    如果打开web.config / app.config文件,您将看到如下内容:

    ...
    <applicationSettings>
      <WebApplication1.Properties.Settings>
        <setting name="MyEndPoints"
                 serializeAs="Xml">
          <value>
            <ArrayOfEndPoint xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                             xmlns:xsd="http://www.w3.org/2001/XMLSchema">
              <EndPoint>
                <HostName>MyHostName</HostName>
                <Port>12345</Port>
              </EndPoint>
              <EndPoint>
                <HostName>MyHost1</HostName>
                <Port>1212</Port>
              </EndPoint>
            </ArrayOfEndPoint>
          </value>
        </setting>
      </WebApplication1.Properties.Settings>
    </applicationSettings>
    ...
    
  7. Finally, to read these settings from your code, simply use

    最后,要从代码中读取这些设置,只需使用即可

    var endPointCollection = Settings.Default.MyEndPoints;
    

    The designer will have created, behind the scenes, the strongly-typed objects to allow the above to work. You can see the full details in the Settings.Designer.cs file.

    设计师将在幕后创建强类型对象以允许上述工作。您可以在Settings.Designer.cs文件中查看完整的详细信息。

Bottom line: you can make all kinds of custom type settings, as long as those settings have XmlSerializable or have type converter. This technique works on Web Applications, WinForms, WPF, Console Applications etc.

底线:您可以进行各种自定义类型设置,只要这些设置具有XmlSerializable或具有类型转换器。此技术适用于Web应用程序,WinForms,WPF,控制台应用程序等。

#2


2  

Here's a variation on the accepted answer, using the following user-defined class to represent a setting:

以下是已接受答案的变体,使用以下用户定义的类来表示设置:

namespace MyApplication
{
    public class EndPoint
    {
        public string HostName { get; set; }
        public int Port { get; set; }
    }
}

The accepted answer proposes the use of a specialised collection class, EndPointCollection to hold settings. However, I don't think this is necessary; an array type (EndPoint[]) also seems to work.

接受的答案建议使用专门的集合类EndPointCollection来保存设置。但是,我不认为这是必要的;数组类型(EndPoint [])似乎也可以工作。

However, typing the array type in the type browser doesn't work; you can instead specify the type directly in the .settings file (using a text editor):

但是,在类型浏览器中键入数组类型不起作用;您可以直接在.se​​ttings文件中指定类型(使用文本编辑器):

<Setting Name="MyEndPoints" Type="MyApplication.EndPoint[]" Scope="User">
    <Value Profile="(Default)" />
</Setting>

Also, if the value editor shown in the accepted answer isn't available, you can instead type the values directly into the value field using XML:

此外,如果接受的答案中显示的值编辑器不可用,您可以使用XML直接在值字段中键入值:

<ArrayOfEndPoint>
    <EndPoint>
        <HostName>MyHostName</HostName>
        <Port>12345</Port>
    </EndPoint>
    <EndPoint>
        <HostName>MyHost1</HostName>
        <Port>1212</Port>
    </EndPoint>
</ArrayOfEndPoint>

A setting with its value set to a complex value using the above XML.

Note that the XML namespace declarations that Visual Studio generates aren't necessary in the XML, as shown above.

请注意,Visual Studio生成的XML名称空间声明在XML中不是必需的,如上所示。


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号