Provides server side configurations to available on client side
To install AspNet.Mvc.ConfigurationExporter;
Install-Package AspNet.Mvc.ConfigurationExporter
Register configuration exporter route;
RouteTable.Routes.MapConfigExporter();
Set configuration export mode;
Mode: SECTION
<add key="configr:Mode" value="Section" />
Add configuration section to web.config;
<section name="exportConfigr" type="AspNet.Mvc.ConfigurationExporter.Section.ConfigrSectionHandler,
AspNet.Mvc.ConfigurationExporter, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
Add configuration section values to web.config;
<exportConfigr>
<appSettings>
<add key="AKey" value="true" />
<add key="BKey" value="false" />
<add key="CKey" value="12" />
<add key="DKey" value="Github" />
<add key="EKey" value="Look ma!" />
</appSettings>
</exportConfigr>
Mode: KEYS
Add following settings to appSettings;
<add key="AKey" value="true" />
<add key="BKey" value="false" />
<add key="CKey" value="12" />
<add key="DKey" value="Github" />
<add key="configr:Keys" value="AKey|BKey|CKey|DKey" />
<add key="configr:Mode" value="Keys" />
Suppose that we have a type and we'd like to make its properties available on client side which marked with ConfigrExportable
public interface ITestConfiguration
{
int TestInt { get; }
string TestString { get; }
int TestProperty { get; }
}
public class TestConfiguration : ITestConfiguration
{
public int TestInt { get { return 20; } }
[ConfigrExportable]
public string TestString { get { return "https://github.com/PanteonProject"; } }
[ConfigrExportable(Name = "testName")]
public int TestProperty { get { return 10; } }
}
Register type to exporter;
Exporter.Instance.RegisterType<ITestConfiguration>(type =>
DependencyResolver.Current.GetService(type));
//OR
Exporter.Instance.RegisterType<ITestConfiguration>(type =>
(ITestConfiguration)new TestConfiguration());
Add following script tag to your page;
<script type="text/javascript" src="~/configr"></script>
Access from js;
console.log(window.configuration.AKey);
Custom JS Namespace
Alternatively you can declare a namespace in App.config like this.
<add key="configr:Namespace" value="github.config" />
and access from js :
console.log(github.config.AKey);