In this article, you will see how to store custom configuration in web.config of an ASP.NET MVC application.
Appsettings
In web.config file you can store configuration information in appsettings
as key value pair and read it in the application.
Store it
<appsettings>
<add key="Appname" value="MyAspnetApp" />
</appsettings>
Read it
System.Configuration.ConfigurationManager.AppSettings["AppName"]
ConnectionStrings
Connection strings can also be stored as key/value pairs in the connectionStrings section of the configuration element of an application configuration file.
Store it
<connectionStrings>
<add name="AppConnection"
providerName="System.Data.ProviderName"
connectionString="Valid Connection String;" />
</connectionStrings>
Read it
System.Configuration.ConfigurationManager.ConnectionStrings["AppConnection"]
Custom Configuration
You can also store custom xml configuration in web.config file. You need to create a custom configuration section handler that inherits from System.Configuration.ConfigurationSection
In this example you will add necessary code to store custom config like this
<webappInfo title="abc company">
<contact name="admin" email="admin@domain.com"/>
<offices>
<add location="india" address="123 india" />
<add location="usa" address="123 usa" />
</offices>
</webappInfo>
This xml information is then mapped to model classes with the help of attributes.
- Create a new asp.net web application (.Net framework) and choose MVC template
- Create new folder
Utils
in the project - Create new file
ContactElement.cs
. This class inherits fromConfigurationElement
. The information fromcontact
element is mapped to this class
public class ContactElement : ConfigurationElement
{
[ConfigurationProperty("name")]
public String Name
{
get
{
return (String)this["name"];
}
set
{
this["name"] = value;
}
}
[ConfigurationProperty("email")]
public String Email
{
get
{
return (String)this["email"];
}
set
{
this["email"] = value;
}
}
}
- Name and Email property are decorated with
ConfigurationProperty
attribute specifying the xml attribute name - Create
OfficeElement.cs
to add information about the offices.
public class OfficeElement : ConfigurationElement
{
[ConfigurationProperty("location")]
public String Location
{
get
{
return (String)this["location"];
}
set
{
this["location"] = value;
}
}
[ConfigurationProperty("address")]
public String Address
{
get
{
return (String)this["address"];
}
set
{
this["address"] = value;
}
}
}
- Create
OfficeCollection.cs
. This class inherits fromConfigurationElementCollection
and is used for adding a collection of office element
public class OfficeCollection: ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new OfficeElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((OfficeElement)element).Location;
}
}
- Create
WebAppInfoSection.cs
. This class inherits fromConfigurationSection
and is used to define custom section in web.config
public class WebAppInfoSection : ConfigurationSection
{
[ConfigurationProperty("title")]
public string Title
{
get
{
return (string)this["title"];
}
set
{
this["title"] = value;
}
}
[ConfigurationProperty("contact")]
public ContactElement Contact
{
get
{
return (ContactElement)this["contact"];
}
set
{ this["contact"] = value; }
}
[ConfigurationProperty("offices")]
[ConfigurationCollection(typeof(OfficeCollection))]
public OfficeCollection Offices
{
get
{
OfficeCollection officeCollection = (OfficeCollection)this["offices"];
return officeCollection;
}
set
{
this["offices"] = value;
}
}
}
- In web.config files, add a new section with name
webappInfo
<configSections>
<section name="webappInfo" type="AspnetMvcCodeSamples.Utils.WebAppInfoSection" />
</configSections>
- Then add custom section configuration
<webappInfo title="abc company">
<contact name="admin" email="admin@domain.com"/>
<offices>
<add location="india" address="123" />
<add location="usa" address="123" />
</offices>
</webappInfo>
- In HomeController.cs, read the section information using
var config = (WebAppInfoSection)System.Configuration.ConfigurationManager.GetSection("webappInfo");
var output = new StringBuilder($"Title: {config.Title}, Contact: {config.Contact.Name} {config.Contact.Email}<br/>");
foreach (var item in config.Offices)
{
var office = (OfficeElement)item;
output.AppendLine($"Office: {office.Location} {office.Address} <br/>");
}
Custom Section Group
You can also add custom section group
<sectionGroup name="webappInfoGroup">
<section
name="webappInfo"
type="AspnetMvcCodeSamples.Utils.WebAppInfoSection"
allowLocation="true"
allowDefinition="Everywhere"
/>
</sectionGroup>
<webappInfoGroup>
<webappInfo title="abc company">
<contact name="admin" email="admin@domain.com"/>
<offices>
<add location="india" address="123" />
<add location="usa" address="123" />
</offices>
</webappInfo>
</webappInfoGroup>
To read it
var config = (WebAppInfoSection)System.Configuration.ConfigurationManager.GetSection("webappInfoGroup/webappInfo ");
Try Sample
Access Code
expeo-in/AspnetAngularCodeSamples: Asp.net Angular Code Samples (github.com)
References
- https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/connection-strings-and-configuration-files
- https://docs.microsoft.com/en-us/previous-versions/aspnet/2tw134k3(v=vs.100)
- https://docs.microsoft.com/en-us/dotnet/api/system.configuration.configurationelementcollection?redirectedfrom=MSDN&view=dotnet-plat-ext-6.0