Create an HttpModule to Process Wildcard Extension Mapping in ASP.NET

Back in July I made a post about configuring IIS to handle Wildcard mapping through ASP.NET. I promised to follow that up with how to write some simple code in ASP.NET to handle the wildcard mapping through a simple URL Mapping httpModule. I am actually going to take it one step further and include an httpHandler to actually produce the content rendered to the browser and send you to a site that proves this works!

So as far as an httpModule I leverage the Init method to register a Handler (BaseModuleRewriter_BeginRequest) for the BeginRequest event. In this method I create a local reference to the httpApplication object, so I can access the Request context to get valuable information about the request.

You need to get the value of the RawURL request and the current ApplicationPath. The Request.RawURL property gets the raw URL of the current request or everything after the domain. For the URL http://www.aspneturl.com/proan, proan would be the RawURL. The ApplicationPath gets the application's virtual path on the server. So http://localhost/aspneturl/proan would produce an ApplicationPath of 'aspneturl'.

Next I perform a check to see if the request was just the VirtualPath, www.aspneturl.com for example. If so then set the requestedPage to the VirtualPath.

Next I change the actual path requested to a special httpHandler (aspneturl.ashx) to actually produce the desired stream of content. I pass a URL parameter in the QueryString that is URL encoded. The QuesryString is a series of parameters and values passed to a URL after a ?. Each set of parameters is separated by an & and each name value pair is related with an =.

This will be used to either produce a message or redirect the user to the actual resource. Finally I call to RewritePath to change the actual URL requested to the one we have now created on the server that will actually request the custom httpHandler. httpContext.RewritePath assigns an internal rewrite path and allows for the URL that is requested to differ from the internal path to the resource.

Public

Class

aspneturlModule

   

Implements

System.Web.IHttpModule

 

   

Public

Sub

Init(

ByVal

context

As

System.Web.HttpApplication) _

       

Implements

System.Web.IHttpModule.Init

 

       

AddHandler

context.BeginRequest, _

                     

AddressOf

Me

.BaseModuleRewriter_BeginRequest

 

   

End

Sub

 

   

Sub

BaseModuleRewriter_BeginRequest(

ByVal

sender

As

Object

,

ByVal

e

As

EventArgs)

 

       

Dim

app

As

HttpApplication =

CType

(sender, HttpApplication)

 

       

Dim

requestedPage

As

String

= app.Request.RawUrl

       

Dim

appVirtualPath

As

String

= app.Request.ApplicationPath

 

       

If

requestedPage.Length >= appVirtualPath.Length

Then

 

           

If

requestedPage.Substring(0, appVirtualPath.Length - 1).ToLower = _

                appVirtualPath.ToLower

Then

 

                requestedPage = requestedPage.Substring(appVirtualPath.Length)

 

           

End

If

 

       

End

If

 

        requestedPage =

String

.Format(

'~/aspneturl.ashx?url={0}'

, _

            HttpUtility.UrlEncode(requestedPage))

 

        HttpContext.Current.RewritePath(requestedPage)

 

   

End

Sub

 

   

Public

Sub

Dispose()

Implements

System.Web.IHttpModule.Dispose

 

   

End

Sub

 

End

Class

Of course we have to register the httpModule with the ASP.NET engine in the web.config file.

<httpModules>

<add type='aspneturlModule' name='aspneturlModule' />

</httpModules>

The custom httpHandler in this case is a very simple demo. There are so many things we could do here to produce content in the user's browser based on the request. Remember that all page requests in ASP.NET are actually handled by custom httpHandlers under the hood. This handler actually looks for a particular value, 'proan' in this case. If that was the request URL, http://www.aspneturl.com/proan then the user is redirected to the http://ProfessionalASPNET.com web site. Otherwise they will see the message or value requested. So if I requested http://www.aspneturl.com/I+Love+ASP.NET then we will see 'I Love ASP.NET' in an H1 tag rendered in the browser.

Back to the checking of the actual requested resource. In this simple example I am looking for one value, we could also hit this against a Hashtable, DataTable, custom collection or any other list and then redirect to the desired location. We could also perform a basic 301 redirect too.

<%@ WebHandler Language='VB' Class='aspneturl' %>

Imports System

Imports System.Web

Public Class aspneturl : Implements IHttpHandler

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

If Not IsNothing(context.Request.QueryString('URL')) Then

Dim sURL As String = HttpUtility.UrlDecode(context.Request.QueryString('URL'))

If sURL = 'proan' Then

context.Response.Redirect('http://www.professionalaspnet.com', True)

Else

context.Response.Write(String.Format('<h1 align=center>{0}</h1>', sURL))

End If

'Else

'context.Response.ContentType = 'text/plain'

' context.Response.Write('You need to enter a URL parameter: ?url=URLENCodedURL')

End If

End Sub

Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable

Get

Return False

End Get

End Property

End Class

Just to prove my point visit a little site I set up http://www.aspneturl.com and test it out. I will soon be adding more functionality to this site so it will perform more like www.TinyURL.com or www.shrinkster.com.

kick it on DotNetKicks.com

Share This Article With Your Friends!