Handling Form Errors with context.RewritePath

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shurlandmoore
    New Member
    • Oct 2011
    • 1

    Handling Form Errors with context.RewritePath

    I recently used HttpContext.Rew ritePath in my global.asx page in asp.net 4.0. The url rewrite worked fine but the was a caveat, when i try to submit the form i got error 404. I realized that the action property of my form was not pointing to the correct url (the most common problem with RewritePath method). So I came up with the following work around. Hope this could help someone who experiencing the same problem.

    my first work around... create a method that finds the current page including querystring. Once the page is found stick it into the form action. You can use the page PreRender event for that. Example


    ''' <summary>
    ''' Gets this page name
    ''' </summary>
    ''' <returns>Curren t Working Page Url</returns>
    Protected Function GetCurrentUrl(B yVal includingQstrin g As Boolean) As String
    Dim Url As String = String.Empty
    If HttpContext.Cur rent Is Nothing Then
    Return Url
    End If

    If includingQstrin g Then
    Dim host As String = ResolveHost(Fal se)
    If host.EndsWith("/") Then
    host = host.Substring( 0, host.Length - 1)
    End If
    Url = host + HttpContext.Cur rent.Request.Ra wUrl
    Else
    Url = HttpContext.Cur rent.Request.Ur l.GetLeftPart(U riPartial.Path)
    End If
    Return Url
    End Function


    ''' <summary>
    ''' Resolves the host location
    ''' </summary>
    ''' <param name="secured"> Use secure socket layer; true/false</param>
    ''' <returns>Stor e host location</returns>
    Protected Function ResolveHost(ByV al secured As Boolean) As String

    Dim retval As String = "http://" & ServerVariables ("HTTP_HOST" )
    Dim prefx = "http://www."

    If Not retval.Contains (www) Then
    If Not HttpContext.Cur rent.Request.Ur l.IsLoopback Then
    retval = retval.Replace( "http://", prefx)
    secured = False
    End If
    End If

    If secured Then
    If Not OwnSSL Then
    retval = SharedSSL
    Else
    retval = retval.Replace( "http:/", "https:/")
    End If
    Else

    retval = retval.Replace( "www.www", "www")
    If Not retval.EndsWith ("/") Then
    retval = retval & "/"
    End If

    Return retval
    End Function

    Now that we've got this function, in the page prerender add the following:

    Protected Overrides Sub OnPreRender(ByV al e As EventArgs)
    form1.Action = Me.GetCurrentUr l(True)
    MyBase.OnPreRen der(e)
    End Sub


    That's it for the first workaround!


    Workaround 2:

    First we need to create a custom form that inherits from HtmlForm Class

    NameSpace RewriteFormWork around
    Public Class MyCustomForm
    Inherits System.Web.UI.H tmlControls.Htm lForm

    Protected Overrides Sub RenderAttribute s(ByVal writer As HtmlTextWriter)
    Attributes.Add( "action", GetCurrentUrl(T rue))
    Attributes.Add( "id", "myform")
    Attributes.Add( "method", "Post")
    Attributes.Rend er(writer)
    End Sub
    End NameSpace

    Add the following directive to your page :
    <%@Register TagPrefix="cf" NameSpace="Rewr iteFormWorkarou nd" Assembly="MyCus tomForm" %>


    Replace your existing form with the following:
    <cf:Form runat="server" id="form1">
    ......
    </cf:Form>


    Hope this helps someone!!
Working...