Read application settings programmatically
Describes how to access Optimizely Content Management System (CMS) settings through the use of a configuration class,
Using a configuration class, you can access Optimizely Content Management System (CMS) settings. Settings are typed members of this class, which gives the added benefit of accessing settings through Intellisense. The class responsible for serving application settings is EPiServer.Configuration.Settings.Instance, defined as:
public static Settings Instance
Accessing the settings through the use of the Instance
property lists settings for the application.
The following example shows how to read and present application settings by using EPiServer.Configuration.Settings.Instance
.
Note
Reading the Optimizely CMS settings programmatically slows down the website.
Code-behind in ApplicationSettingsPage.aspx.cs
using System;
using System.Text;
using System.Collections.Generic;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using EPiServer;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.Web.WebControls;
using EPiServer.Configuration;
using EPiServer.Web;
namespace CodeSamples {
public partial class ApplicationSettingsPage: EPiServer.TemplatePage {
protected void Page_Load(object sender, EventArgs e) {
lblUrlRewriteExtension.Text = Settings.Instance.UrlRewriteExtension;
lblUIUrl.Text = Settings.Instance.UIUrl.ToString();
// To read custom settings from the <appSettings> block in web.config
// use code similar to:
string customSetting = System.Web.Configuration.WebConfigurationManager.AppSettings["myCustomSetting"];
}
}
}
Markup in ApplicationSettingsPage.aspx
<table cellspacing="4" border="1">
<tr>
<td>UrlRewriteExtension</td>
<td><asp:label id="lblUrlRewriteExtension" runat="server" text="Label" /></td>
</tr>
<tr>
<td>The UI URL</td>
<td><asp:Label id="lblUIUrl" runat="server" /></td>
</tr>
</table>
The output from this code should resemble something like the following (depending on the actual configuration):
UrlRewriteExtension | .htm |
---|---|
The UI URL | ~/EpiServerTest/ui/ |
Updated 8 months ago