Custom type application settings in ASP.NET

前端 未结 2 1643
情歌与酒
情歌与酒 2020-12-08 23:52

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 onl

2条回答
  •  自闭症患者
    2020-12-09 00:26

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

    
        
    
    

    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:

    
        
            MyHostName
            12345
        
        
            MyHost1
            1212
        
    
    

    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.

提交回复
热议问题