Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 366
naveen Avatar asked Jun 09 '11 07:06

naveen


People also ask

Where are .NET application settings stored?

Settings. settings is located in the My Project folder for Visual Basic projects and in the Properties folder for Visual C# projects. The Project Designer then searches for other settings files in the project's root folder. Therefore, you should put your custom settings file there.

What are application settings?

Application settings enables developers to save state in their application using very little custom code, and is a replacement for dynamic properties in previous versions of the . NET Framework.

Where is the application settings?

From the Home screen, tap the Apps icon (in the QuickTap Bar) > the Apps tab (if necessary) > Settings .


2 Answers

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.

like image 139
Alex Aza Avatar answered Oct 19 '22 06:10

Alex Aza


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.

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):

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

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

like image 3
Sam Avatar answered Oct 19 '22 05:10

Sam