February 2, 2015 07:10 by
Peter
In this article, I will explain you about creating a custom configuration sections in ASP.NET 5. We want to extend ASP.NET configuration settings using XML configuration elements of your own. To do this, we produce a custom configuration section handler. The handler should be a .NET Framework class that inherits from the System.Configuration.ConfigurationSection class. The section handler interprets and processes the settings that are outlined in XML configuration elements in a specific section of a web.config file. We willl browse and write these settings through the handler's properties. And here is the code that I used:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
namespace CustomConfigSection
{
public class LoginRedirectByRoleSection : ConfigurationSection
{
[ConfigurationProperty("roleRedirects")]
public RoleRedirectCollection RoleRedirects
{
get
{
return (RoleRedirectCollection)this["roleRedirects"];
}
set
{
this["roleRedirects"] = value;
}
}
}
public class RoleRedirect : ConfigurationElement
{
[ConfigurationProperty("role", IsRequired = true)]
public string Role
{
get
{
return (string)this["role"];
}
set
{
this["role"] = value;
}
}
[ConfigurationProperty("url", IsRequired = true)]
public string Url
{
get
{
return (string)this["url"];
}
set
{
this["url"] = value;
}
}
}
public class RoleRedirectCollection : ConfigurationElementCollection
{
public RoleRedirect this[int index]
{
get
{
return (RoleRedirect)BaseGet(index);
}
}
public RoleRedirect this[object key]
{
get
{
return (RoleRedirect)BaseGet(key);
}
}
protected override ConfigurationElement CreateNewElement()
{
return new RoleRedirect();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((RoleRedirect)element).Role;
}
}
}
<configSections>
<section name="loginRedirectByRole" type="CustomConfigSection.LoginRedirectByRoleSection,CustomConfigSection" allowLocation="true" allowDefinition="Everywhere" />
</configSections>
<loginRedirectByRole>
<roleRedirects>
<add role="Administrator" url="~/Admin/Default.aspx" />
<add role="User" url="~/User/Default.aspx" />
</roleRedirects>
</loginRedirectByRole>
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using CustomConfigSection;
namespace CustomConfigSection
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
LoginRedirectByRoleSection roleRedirectSection = (LoginRedirectByRoleSection)ConfigurationManager.GetSection("loginRedirectByRole");
foreach (RoleRedirect roleRedirect in roleRedirectSection.RoleRedirects)
{
if (Roles.IsUserInRole("", roleRedirect.Role))
{
Response.Redirect(roleRedirect.Url);
}
}
}
}
}
HostForLIFE.eu ASP.NET 5 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.