Click here to Skip to main content
15,885,954 members
Articles / Web Development / ASP.NET

Solution for URL Rewriting in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.24/5 (13 votes)
3 Jul 2007CPOL8 min read 109.7K   803   69  
Powerful solution for URL rewriting and handling rewritten parameters in ASP.NET.
Imports Microsoft.VisualBasic

Namespace Rewriting

    Public Class FormRewriterControlAdapter
        Inherits System.Web.UI.Adapters.ControlAdapter

        Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
            MyBase.Render(New RewriteFormHtmlTextWriter(writer))
        End Sub

    End Class

    Public Class RewriteFormHtmlTextWriter
        Inherits HtmlTextWriter

        Sub New(ByVal writer As HtmlTextWriter)
            MyBase.New(writer)
            Me.InnerWriter = writer.InnerWriter
        End Sub

        Sub New(ByVal writer As System.IO.TextWriter)
            MyBase.New(writer)
            MyBase.InnerWriter = writer
        End Sub

        Public Overrides Sub WriteAttribute(ByVal name As String, ByVal value As String, ByVal fEncode As Boolean)

            ' If the attribute we are writing is the "action" attribute, and we are not on a sub-control, 
            ' then replace the value to write with the raw URL of the request - which ensures that we'll
            ' preserve the PathInfo value on postback scenarios

            If (name = "action") Then

                Dim Context As HttpContext
                Context = HttpContext.Current

                If Context.Items("ActionAlreadyWritten") Is Nothing Then

                    ' Because we are using the UrlRewriting.net HttpModule, we will use the 
                    ' Request.RawUrl property within ASP.NET to retrieve the origional URL
                    ' before it was re-written.  You'll want to change the line of code below
                    ' if you use a different URL rewriting implementation.

                    value = Context.Request.RawUrl

                    ' Indicate that we've already rewritten the <form>'s action attribute to prevent
                    ' us from rewriting a sub-control under the <form> control

                    Context.Items("ActionAlreadyWritten") = True

                End If

            End If

            MyBase.WriteAttribute(name, value, fEncode)

        End Sub

    End Class


End Namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions