url mapping problem

Feb 26 2008 8:39 AM
hi.
here is the code.
1.   Sub context_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)

        Dim app As System.Web.HttpApplication = CType(sender, System.Web.HttpApplication)
        Dim urlSource As String = app.Request.Path
        Dim urlToChangeTo As String = MyChangeToFunction(urlSource)
        app.Context.RewritePath(urlToChangeTo)
    End Sub

2.   Function MyChangeToFunction(ByVal source As String) As String
        Try
            Dim pos As Integer = -1
            Dim Dir As String
            Dim DirId As String = ""
            Dim Dir1 As String
            Dim sql As String
            Dim Dir4 As String = ""
            Dim Dir2 As String
            Dim pos1 As Integer = -1
            Dim pos3 As Integer = -1
            Dim Dir3 As String = ""

            Dir4 = source.Substring(1, source.Length - 1)
            pos1 = Convert.ToInt32(Dir4.IndexOf("/"))

            pos = Convert.ToInt32(source.LastIndexOf("/"))
            Dir = Dir4.Substring(0, pos1)

            Dir1 = Dir4.Substring(pos1)

            Dir2 = Dir + "/Client" + Dir1
            pos3 = Convert.ToInt32(Dir1.LastIndexOf("/"))
            Dir3 = Dir1.Substring(pos3 + 1)

            Dim scon As New SqlConnection
            Dim scom As SqlCommand

            Dim sdr As SqlDataReader
            scon = New SqlConnection("server=devlpc27;" _
                                             & "Initial Catalog=db_agrierp;" _
                                              & "User Id=sa;" _
                                              & "Password=veryd;")

            sql = "Select pkid from tbl_directories where directory = '" & Dir3 & "'"
            scom = New SqlCommand(sql, scon)
            scon.Open()
            sdr = scom.ExecuteReader
            If sdr.HasRows Then
                While sdr.Read
                    DirId = sdr("pkid").ToString
                End While
            End If
            scon.Close()

            Dim QueryStyring As String = source.Substring(source.LastIndexOf("/"c) + 1)
            If QueryStyring = "" Then
                Return source
            End If
            Dim ext As String = QueryStyring.Substring(QueryStyring.IndexOf("."c) + 1)
            If ext = "gif" OrElse ext = "axd" OrElse ext = "css" OrElse ext = "jpg" Then
                Return source
            Else

                If QueryStyring = "Default.aspx" Then
                    Return source
                Else
                    Dim path As String
                    'Dim path As String = source.Substring(0, source.LastIndexOf("/"c))
                    path = Dir2 & "/Default.aspx"
                    'path = Dir2 & "/Default.aspx?Id ='" & DirId & "'"
                    Return path

                End If
            End If


            'If Not IsNothing(HttpContext.Current.Session("UserID")) Then
            '    Dim UID As String = HttpContext.Current.Session("UserID")
            '    '
            '    If UID = "" Then
            '        Return source
            '    Else
            '        Return source.Insert(source.IndexOf("/"c), UID & "/")
            '    End If
            'Else
            '    Return source
            'End If

        Catch ex As Exception
            Return source
        End Try

    End Function

now,
my problem is that, when  i run the application with path- agrierp/hcl/hcl1, then
above two function will work and return updated path that is- agrierp/client/hcl/hcl1/default.aspx which  i want but when  the whole application  execute, it shows agrierp/hcl/agrierp/hcl/hcl1/default.aspx means first two word get repeat.
is ther any solution for logical url mapping?
thanks