473,503 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Handling Form Errors with context.RewritePath

1 New Member
I recently used HttpContext.RewritePath 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>Current Working Page Url</returns>
Protected Function GetCurrentUrl(ByVal includingQstring As Boolean) As String
Dim Url As String = String.Empty
If HttpContext.Current Is Nothing Then
Return Url
End If

If includingQstring Then
Dim host As String = ResolveHost(False)
If host.EndsWith("/") Then
host = host.Substring(0, host.Length - 1)
End If
Url = host + HttpContext.Current.Request.RawUrl
Else
Url = HttpContext.Current.Request.Url.GetLeftPart(UriPar tial.Path)
End If
Return Url
End Function


''' <summary>
''' Resolves the host location
''' </summary>
''' <param name="secured">Use secure socket layer; true/false</param>
''' <returns>Store host location</returns>
Protected Function ResolveHost(ByVal 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.Current.Request.Url.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(ByVal e As EventArgs)
form1.Action = Me.GetCurrentUrl(True)
MyBase.OnPreRender(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 RewriteFormWorkaround
Public Class MyCustomForm
Inherits System.Web.UI.HtmlControls.HtmlForm

Protected Overrides Sub RenderAttributes(ByVal writer As HtmlTextWriter)
Attributes.Add("action", GetCurrentUrl(True))
Attributes.Add("id", "myform")
Attributes.Add("method", "Post")
Attributes.Render(writer)
End Sub
End NameSpace

Add the following directive to your page :
<%@Register TagPrefix="cf" NameSpace="RewriteFormWorkaround" Assembly="MyCustomForm" %>


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


Hope this helps someone!!
Oct 28 '11 #1
0 3448

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1637
by: Dan Foody | last post by:
I've embedded a .NET 1.1 control in a web page for use in IE (using the "object" tag). There are two challenges I'm facing: if the machine does not have the 1.1 framework installed or if the .NET...
0
2235
by: Matt Howeson | last post by:
Some time ago I posted a request for help with a problem I was having sometime ago whereby a 404 error would result if any access to the Querystring had been made before the Context.Rewritepath is...
0
2640
by: Ayende Rahien | last post by:
I've a very annoying problem, I'm using Application_BeginRequest() and Context.RewritePath to hide query strings. The problem that I've is that a form that I submit to /Comments/17.aspx has form...
0
1493
by: Ayende Rahien | last post by:
I've a very annoying problem, I'm using Application_BeginRequest() and Context.RewritePath to hide query strings. The problem that I've is that a form that I submit to /Comments/17.aspx has form...
1
3097
by: Oytun YILMAZ | last post by:
Hi I thing I have found a critical bug about ASP.NET ReWritePath Function Here are the steps to reproduce: I have a solution with this items: "MasterPageWithoutUserControl.aspx"...
1
2430
by: Alan Silver | last post by:
Hello, I have a page where site owners can see orders placed on their site. The path to this page is /dap/order.aspx, but for security reasons (as they will end up printing these pages and...
3
2407
by: MatsL | last post by:
Hi, I'm using Context.RewritePath() in a HttpModule to get url rewriting done. http://localhost/Web1/articles -> http://localhost/Web1/Default.aspx?m=articles But I'm having problems with...
3
2261
by: hardieca | last post by:
Hi, I've created an n-tier app where validation rules reside in the business layer. When a webform is saved, a business object examines its state, and if some property is invalid, throws a...
0
1394
by: Andrew2 | last post by:
Sorry guy for the (probably) stupid question, but reading and testing the second overload of the Context.RewritePath, that one with the rebaseClientPath param. Msdn says: true to reset the...
1
3447
by: Keef | last post by:
Hiya... just wondering if anyone else has come across this problem, and if there is a solution... i'm writing a reasonably simple IHttpModule to rewrite URLs ... in the BeginRequest event i...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7460
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.