Question Question about HttpModule and Rewriting

r3plica

Well-known member
Joined
Mar 4, 2010
Messages
86
Programming Experience
3-5
Good morning all,

I have run into this problem a few times and it is quite frankly driving me nuts.
First I will paste my source code and then I will talk about my issue a little.

VB.NET:
Imports Microsoft.VisualBasic
Imports HoN.Common
Imports HoN.Business.ItemBusiness
Imports HoN.Business.ModificationBusiness

Public Class UrlRewrite
    Implements IHttpModule

    Public Sub Dispose() Implements IHttpModule.Dispose

    End Sub

    Public Sub Init(ByVal context As HttpApplication) Implements IHttpModule.Init
        AddHandler context.AuthorizeRequest, AddressOf OnAuthorizeRequest
    End Sub

    Private Sub OnAuthorizeRequest(ByVal s As Object, ByVal e As EventArgs)
        CheckURL(CType(s, HttpApplication))
    End Sub

    Private Sub CheckURL(ByVal app As HttpApplication)
        Dim pLocationCol As LocationCollection = ReturnItemLocations()
        Dim pItemCol As ItemCollection = ReturnAllItems()
        Dim pModCol As ModificationCollection = ReturnAllModifications()

        For Each pLocation As Location In pLocationCol
            If app.Request.RawUrl.ToLower.Contains("/items/" & Replace(Replace(pLocation.Name.ToLower, " ", ""), "'", "") & ".aspx") Then
                app.Context.RewritePath("~/Results.aspx", "", "i=" & pLocation.ID)
            End If
        Next

        For Each pItem As Item In pItemCol
            If app.Request.RawUrl.ToLower.Contains("/item/" & Replace(Replace(pItem.Name.ToLower, " ", ""), "'", "")) Then
                app.Context.RewritePath("~/Item.aspx", "", "i=" & pItem.ID)
            End If
        Next

        For Each pMod As Modification In pModCol
            If app.Request.RawUrl.ToLower.Contains("/mods/" & Replace(Replace(pMod.Name.ToLower, " ", ""), "'", "")) Then
                app.Context.RewritePath("~/Modification.aspx", "", "i=" & pMod.ID)
            End If
        Next
    End Sub
End Class

First of all, I have the references needed in my web.config and my URL rewriting works, but it is not stable.
What I have found is that because HttpModule is fired mulitple times on a sinlge page it runs the section below everytime.
The collections are retrieved from a database.

VB.NET:
        Dim pLocationCol As LocationCollection = ReturnItemLocations()
        Dim pItemCol As ItemCollection = ReturnAllItems()
        Dim pModCol As ModificationCollection = ReturnAllModifications()

What happens is that the requests happen so fast, so sometimes the connection is still opening when the second request comes through, or it moans stating that it can't find column one, etc. The result is my page comes back with an error and the page isnt displayed.

I need to somehow either make "CheckURL" run only once per page request regardless of the objects on that page OR find a way to store the collections in memory (like sessions, except I realise I can't use sessions here).

Please please please help me :) or point me in the right direction.

If there is anything I have missed, please let me know.
 
I am not sure if anyone is interested in this, but I found the solution.
I tried to use Sessions, but they are not available to me but Caching is.
Becuase the collections do not change often (once a month maximum) it was the best thing to do.
Now my code looks like:

VB.NET:
Imports Microsoft.VisualBasic
Imports HoN.Common
Imports HoN.Business.ItemBusiness
Imports HoN.Business.ModificationBusiness

Public Class UrlRewrite
    Implements IHttpModule

    Public Sub Dispose() Implements IHttpModule.Dispose

    End Sub

    Public Sub Init(ByVal context As HttpApplication) Implements IHttpModule.Init
        AddHandler context.AuthorizeRequest, AddressOf OnAuthorizeRequest
    End Sub

    Private Sub OnAuthorizeRequest(ByVal s As Object, ByVal e As EventArgs)
        ' Dim ctx As HttpContext = HttpContext.Current
        CheckURL(CType(s, HttpApplication))
    End Sub

    Private Sub CheckURL(ByVal app As HttpApplication)
        If HttpContext.Current.Cache("LocationCollection") Is Nothing Then HttpContext.Current.Cache.Insert("LocationCollection", ReturnItemLocations())
        If HttpContext.Current.Cache("ItemCollection") Is Nothing Then HttpContext.Current.Cache.Insert("ItemCollection", ReturnAllItems())
        If HttpContext.Current.Cache("ModificationCollection") Is Nothing Then HttpContext.Current.Cache.Insert("ModificationCollection", ReturnAllModifications())

        If app.Request.RawUrl.ToLower.Contains("/items/") Then
            For Each pLocation As Location In CType(HttpContext.Current.Cache("LocationCollection"), LocationCollection)
                If app.Request.RawUrl.ToLower.Contains("/items/" & Replace(Replace(pLocation.Name.ToLower, " ", ""), "'", "") & ".aspx") Then
                    app.Context.RewritePath("~/Results.aspx", "", "i=" & pLocation.ID)
                End If
            Next
        End If

        If app.Request.RawUrl.ToLower.Contains("/item/") Then
            For Each pItem As Item In CType(HttpContext.Current.Cache("ItemCollection"), ItemCollection)
                If app.Request.RawUrl.ToLower.Contains("/item/" & Replace(Replace(pItem.Name.ToLower, " ", ""), "'", "")) Then
                    app.Context.RewritePath("~/Item.aspx", "", "i=" & pItem.ID)
                End If
            Next
        End If

        If app.Request.RawUrl.ToLower.Contains("/mods/") Then
            For Each pMod As Modification In CType(HttpContext.Current.Cache("ModificationCollection"), ModificationCollection)
                If app.Request.RawUrl.ToLower.Contains("/mods/" & Replace(Replace(pMod.Name.ToLower, " ", ""), "'", "")) Then
                    app.Context.RewritePath("~/Modification.aspx", "", "i=" & pMod.ID)
                End If
            Next
        End If
    End Sub
End Class

and it works perfectly.
 
Back
Top