Sunday, November 18, 2007

Sunday, November 11, 2007

Custom configuration section in asp.net 2.0

My next proof of concept is to build a custom URLMapping. The URLMapping that comes with asp.net does allow us to map based on regular expression or custom logic.

For eg. If I want to map a url http:///www.town.com/sp/lp/lng/ja/ctr/jp/default.aspx to
http:///www.town.com/sp/lp/default.aspx?ct=jp&ln=ja
or
say I have an url http://www.town.com/sp/lp/overview.aspx but internally I have overview.aspx for each country - you would like to configure and want to rewrite the url as http://www.town.com/sp/lp/jp/overview.aspx if the user come from Japan.

To build this first I need to build a custom config section handler and then a http context module or HttpHandler Factory to handle the mapping and do necessary action.

Thought of blogging a few concepts which were related to my POC before blogging about my POC itself. Below section covers how to write a custom configuration section handler in .net 2.0. For my POC, I had to include a custom config section. Something similar to below

<urlConfigSections>
<urlMappings enabled="true">
<urlMapping url="^/urlmapping/lp/(.*)/(.*).aspx" mappedUrl="~/urlmapping/lp/$2.aspx?ct=$1&ln=$2" isHandlerMapped="false" />
<urlMapping url=".*/module/(.*)/default.aspx" mappedUrl="~/$1/default.aspx" isHandlerMapped="false" />
<urlMapping url=".*/seo/(.*)/default.aspx" isHandlerMapped="true" mappedType="Town.Www.Sp.Controller.SEOHandler, Town.Www.Sp"/>
</urlMappings>
</urlConfigSections>


So, I had to write a custom config handler. It is pretty straight forward in .net 2.0

In our case, we have three things to worry. One is newly added configuration section, second the new Element Collection (in our case it is <urlMappings>) and finally each element in the collection (in our case it is <urlMapping>). Below are the list of class we must inherit from and give our custom implementation
  • System.Configuration.ConfigurationSection
  • System.Configuration.ConfigurationElement
  • System.Configuration.ConfigurationElementCollection
Below is the complete code snippet.
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Configuration

Namespace Configuration
''' @Author: Praveen
''' @Date created: 11/10/2007
''' Intention of this class to handle the custom config section that conceptually
''' implement the URLMapping config section in ASP.Net 2.0 but support regular expression
'''
'''


Public NotInheritable Class UrlMappingConfigurationSection
: Inherits System.Configuration.ConfigurationSection

Public Property IsEnabled() As Boolean
Get
Return CBool(Me("enabled"))
End Get

Set(ByVal value As Boolean)
Me("enabled") = value
End Set
End Property

Public ReadOnly Property CustomUrlMappings() As UrlMappingCollection
Get
Return CType(MyBase.Item("urlMappings"), UrlMappingCollection)
End Get
End Property

Public NotInheritable Class UrlMapping
: Inherits System.Configuration.ConfigurationElement

Public Property Url() As String
Get
Return CStr(Me("url"))
End Get
Set(ByVal value As String)
Me("url") = value
End Set
End Property

Public Property MappedUrl() As String
Get
Return CStr(Me("mappedUrl"))
End Get

Set(ByVal value As String)
Me("mappedUrl") = value
End Set
End Property

Public Property IsHandlerMapped() As Boolean
Get
Return CBool(Me("isHandlerMapped"))
End Get

Set(ByVal value As Boolean)
Me("isHandlerMapped") = value
End Set
End Property

Public Property MappedType() As String
Get
Return CStr(Me("mappedType"))
End Get

Set(ByVal value As String)
Me("mappedType") = value
End Set
End Property

Public Property IsEnabled() As Boolean
Get
Return CBool(Me("enabled"))
End Get

Set(ByVal value As Boolean)
Me("enabled") = value
End Set
End Property

End Class

Public NotInheritable Class UrlMappingCollection
: Inherits System.Configuration.ConfigurationElementCollection
Protected Overloads Overrides Function CreateNewElement() As ConfigurationElement
Return New UrlMapping()
End Function

Protected Overrides Function GetElementKey(ByVal element As ConfigurationElement) As Object
Return (CType(element, UrlMapping)).Url
End Function

Public Overrides ReadOnly Property CollectionType() As ConfigurationElementCollectionType
Get
Return ConfigurationElementCollectionType.BasicMap
End Get
End Property

Protected Overrides ReadOnly Property ElementName() As String
Get
Return "urlMapping"
End Get
End Property
End Class

End Class

End Namespace
My next part would be read the web.config custom section and handle them appropriate for specific business rule