Search code examples
asp.net-mvcmodel-view-controllerprofile

Get the profile properties names from web config


I need to get all the profile properties from web.config file

<profile defaultProvider="AspNetSqlProfileProvider" automaticSaveEnabled="false" enabled="true">
<providers>
    <clear/>
    <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ApplicationServices" applicationName="/" />
</providers>
<properties>
    <add name="FirstName" type="string" />
    <add name="LastName" type="string" />
    <add name="Company" type="string" />
</properties>

I want to get the array with values ['FirstName','LastName','Company']


Solution

  • This should get you the array:

    var configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/aspnet");
    
    var profileSection = (System.Web.Configuration.ProfileSection)configuration.GetSection("system.web/profile");
    
    var properties = profileSection.PropertySettings.AllKeys; //FirstName, LastName, Company
    

    More information on ProfileSection: https://msdn.microsoft.com/en-us/library/system.web.configuration.profilesection(v=vs.100).aspx