Microsoft Most Valuable Professional

Chris Pietschmann

An MVP From Wisconsin



URL Mapping for ASP.NET 1.1

With the help of an http module, a custom config handler and a few lines of code we can add the same ASP.NET 2.0 style URL Mapping to our ASP.NET 1.1 apps.

1) First lets start by creating a new Class Library project named 'URLMapping_HTTPModule'

2) Then create three files as listed below:

BaseModuleRewriter.vb

Imports System.Web

Public Class BaseModuleRewriter
    Implements System.Web.IHttpModule

    Sub Init(ByVal app As HttpApplication) Implements IHttpModule.Init
        AddHandler app.AuthorizeRequest, AddressOf Me.BaseModuleRewriter_AuthorizeRequest
    End Sub

    Sub Dispose() Implements System.Web.IHttpModule.Dispose
    End Sub

    Sub BaseModuleRewriter_AuthorizeRequest(ByVal sender As Object, ByVal e As EventArgs)
        Dim app As HttpApplication = CType(sender, HttpApplication)
        Rewrite(app.Request.Path, app)
    End Sub

    Overridable Sub Rewrite(ByVal requestedPath As String, ByVal app As HttpApplication)
    End Sub

End Class

URLMappingModule.vb

Imports System.Web
Imports System.Configuration

Public Class URLMappingModule
    Inherits BaseModuleRewriter

    Overrides Sub Rewrite(ByVal requestedPath As String, ByVal app As HttpApplication)
        ''Implement functionality here that mimics the 'URL Mapping' features of ASP.NET 2.0
        Dim config As UrlMappingsConfigHandler = CType(ConfigurationSettings.GetConfig("system.web/urlMappings"), UrlMappingsConfigHandler)

        Dim pathOld As String, queryStringArg As String, pathNew As String = ""
        If config.Enabled Then
            pathOld = app.Request.RawUrl

            ''Get the request page without the querystring parameters
            Dim requestedPage As String = app.Request.RawUrl.ToLower
            If requestedPage.IndexOf("?") > -1 Then
                requestedPage = requestedPage.Substring(0, requestedPage.IndexOf("?"))
            End If

            ''Format the requested page (url) to have a ~ instead of the virtual path of the app
            Dim appVirtualPath As String = app.Request.ApplicationPath
            If requestedPage.Length >= appVirtualPath.Length Then
                If requestedPage.Substring(0, appVirtualPath.Length).ToLower = appVirtualPath.ToLower Then
                    requestedPage = requestedPage.Substring(appVirtualPath.Length)
                    If requestedPage.Substring(0, 1) = "/" Then
                        requestedPage = "~" & requestedPage
                    Else
                        requestedPage = "~/" & requestedPage
                    End If
                End If
            End If

            ''Get the new path to rewrite the url to if it meets one
            ''of the defined virtual urls.
            pathNew = config.MappedUrl(requestedPage)

            ''If the requested url matches one of the virtual one
            ''the lets go and rewrite it.
            If pathNew.Length > 0 Then
                If pathNew.IndexOf("?") > -1 Then
                    ''The matched page has a querystring defined
                    If pathOld.IndexOf("?") > -1 Then
                        pathNew += "&" & Right(pathOld, pathOld.Length - pathOld.IndexOf("?") - 1)
                    End If
                Else
                    ''The matched page doesn't have a querystring defined
                    If pathOld.IndexOf("?") > -1 Then
                        pathNew += Right(pathOld, pathOld.Length - pathOld.IndexOf("?"))
                    End If
                End If

                ''Rewrite to the new url
                app.Context.Current.RewritePath(pathNew)
            End If

        End If
    End Sub

End Class

UrlMappingsConfigHandler.vb

Imports System.Configuration
Imports System.Xml

Public Class UrlMappingsConfigHandler
Implements IConfigurationSectionHandler
Dim _Section As XmlNode

Public Function Create(ByVal parent As Object, ByVal configContext As Object, ByVal section As System.Xml.XmlNode) As Object Implements System.Configuration.IConfigurationSectionHandler.Create
    _Section = section
    Return Me
End Function

Friend Function Enabled() As Boolean
    ''' Get whether url mapping is enabled in the app.config
    If _Section.Attributes("enabled").Value.ToLower = "true" Then
        Return True
    Else
        Return False
    End If
End Function

Friend Function MappedUrl(ByVal url As String) As String
    ''' Get the matching "mapped Url" from the web.config file if there is one.
    Dim x As XmlNode
    Dim _mappedURL As String = ""
    For Each x In _Section.ChildNodes
        If url.ToLower = x.Attributes("url").Value.ToLower Then
            _mappedURL = x.Attributes("mappedUrl").Value
        End If
    Next
    Return _mappedURL
End Function

3) Now in the ASP.NET 1.1 app that you would like to use this in, just add the following lines to your web.config file:

[code:html]
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <!-- Declare the custom 'urlMappings' section and handler -->
 <configSections>
        <sectionGroup name="system.web">
            <section name="urlMappings" type="URLMapping_HTTPModule.UrlMappingsConfigHandler,URLMapping_HTTPModule"/>
        </sectionGroup>
    </configSections>
   
 <system.web>
  <!-- Tell ASP.NET to use the URL Mapping HTTP Module -->
  <httpModules>
   <add type="URLMapping_HTTPModule.URLMappingModule, URLMapping_HTTPModule" name="URLMappingModule" />
  </httpModules>
 
  <!-- This is the custom 'urlMappings' section -->
  <urlMappings enabled="true">
   <add
    url="~/Chris.aspx"
    mappedUrl="~/Default.aspx?p=chris" />
   <add
    url="~/Kate.aspx"
    mappedUrl="~/Default.aspx?p=kate" />
   <add
    url="~/folder/test.aspx"
    mappedUrl="~/Default.aspx?p=foldertest" />
  </urlMappings>
 </system.web>
</configuration>
[/code]

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: General
Posted by crpietschmann on Monday, July 04, 2005 7:51 PM
Permalink | Comments (6) | Post RSSRSS comment feed

Related posts

Comments

Christopher Pietschmann, MCSD, MCAD

Thursday, July 21, 2005 4:10 PM

Christopher Pietschmann, MCSD, MCAD

Since you say it worked in http://www.microsoft.com/net/" target="_blank">.NET 1.1, I'm not sure. Just make sure you have the right info in the right places.

The layout of it needs to be:
<add type="classname, assemblyname" name="modulename"/>

Is "PageRefresh" the name of your assembly? Is your assembly compiled to PageRefresh.dll? My guess is that you may not have the right info in the right place.

Bilal Haidar

Thursday, July 21, 2005 4:10 PM

Bilal Haidar

Hello. I am Bilal Haidar, a http://Microsoft.com" target="_blank">Microsoft MVP. I saw your post on URL Mapping. I wonder I have created a new class in VS.NET 2005. The namespace is 'PageRefresh' and the HttpModule called 'ApplicationModule', I added this
to the Web.Config:

<system.web>
<httpModules>
<add name="ApplicationModule" type="PageRefresh.ApplicationModule ,
PageRefresh" />
</httpModules>
</system.web>

It gives me a Configuration Error. That worked fine in http://asp.net" target="_blank">ASP.NET 1.1, any
idea?

Thank you

Christopher Pietschmann, MCSD, MCAD

Saturday, November 12, 2005 3:39 PM

Christopher Pietschmann, MCSD, MCAD

I have ported this implementation over to http://asp.net" target="_blank">ASP.NET 2.0 and added Regular Expression support.

pietschsoft.com/Blog/archive/2005/11/12/762.aspx" rel="nofollow">pietschsoft.com/Blog/archive/2005/11/12/762.aspx">pietschsoft.com/Blog/archive/2005/11/12/762.aspx" rel="nofollow">pietschsoft.com/Blog/archive/2005/11/12/762.aspx

Rich Freeman

Thursday, January 12, 2006 1:22 AM

Rich Freeman

Great article. I'd like to modify it so that instead of using the web.config, the URL mappings are generated dynamically in an external XML file. I can create my XML file, but would appreciate any advice you have on how to modify your code to read from this file rather than from the web.config. Thanks!

Simon

Thursday, January 19, 2006 8:41 PM

Simon

I have used the above code, it works fine to rediret to url's without parameters, but when adding a parameter, eg ID=3 im faced with a "page cannot be displayed", can anyone help?

Will

Thursday, February 02, 2006 5:20 PM

Will

There is an easier way to create a permalink structure. All you need is about 12 lines of code in your Global.asax file.

www.willasrari.com/.../

Comments are closed

About the author

I'm Chris Pietschmann, go to the About Me page to learn more about me.

Search

Sponsors

Web.Maps.VE - ASP.NET AJAX Virtual Earth Mapping Server Control

Recent comments

Disclaimer


This work is licensed under a Creative Commons Attribution 3.0 United States License, unless explicitly stated otherwise within the posted content.
© Copyright 2004 - 2008 Chris Pietschmann