Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

c# - Custom type application settings in 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

  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.

    Visual Studio's settings value editor

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

    ...
    <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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...