ASP.NET postbacks and URL rewriting
ASP.NET Web Forms extensively use postback mechanism in order to maintain the state of the server-side controls on the web page. This makes it somewhat tricky to perform URL rewriting for ASP.NET pages. When a server side form control is added to the web page, ASP.NET will render the response with HTML <form> tag that contains an action attribute pointing back to the page where the form control is. This means that if URL rewriting was used for that page, the action attribute will point back to the rewritten URL, not to the URL that was requested from the browser. This will cause the browser to show rewritten URL any time a postback occurs.
Let me demonstrate this on an example. Assume you have a very simple web form in a file called default.aspx. When you request http://localhost/default.aspx in a browser and then view the HTML source for the response, you will see that the response contains the <form> element, which looks similar to this:
<form name="form1" method="post" action="Default.aspx" id="form1">
The action attribute contains the URL where the form data will be posted to when you click on the button in the web page.
Now, let’s create a very simple rewrite rule that rewrites URL from http://localhost/homepage to http://localhost/default.aspx.
<rewrite> <rules> <rule name="MyRule" patternSyntax="Wildcard"> <match url="homepage" /> <action type="Rewrite" url="default.aspx" /> </rule> </rules> </rewrite>
When you request http://localhost/homepage in a browser, the URL will be rewritten in accordance to the above rule and the page will be shown correctly in the browser:
However, when you click on the Submit button, the browser’s address bar will display http://localhost/default.aspx, thus exposing the internal URL, that you wanted to hide by using URL rewriting:
A few workarounds to fix this behavior existed in previous versions of ASP.NET. For example, you could sub-class the form control, or use Control Adapter as explained here. However, these workarounds were not very easy to implement. Luckily, the ASP.NET in .NET Framework 3.5 SP1 provides a very simple way to fix that. Now you can use the property of the HtmlForm class called Action to set the postback URL to the one that was requested by browser before any rewriting happened. In ASP.NET you can obtain that URL by using HttpRequest.RawUrl property. So, to fix the postback URL for your web form when you use URL Rewrite Module, you would need to add the following code to the page:
protected void Page_Load(object sender, EventArgs e)
{
form1.Action = Request.RawUrl;
}
After this change, if you reload the web page at http://localhost/homepage and then click on submit button you will see that the browser’s address bar still displays the correct URL:
If you view the HTML for the response you will see that the <form> tag now contains the correct action URL:
<form name="form1" method="post" action="/homepage" id="form1">
When you use ASP.NET master pages you could add the Page_Load method to the master page and that would take care of postback action URL for all the pages in your web application. Note thought that in order to be able to use the HtmlForm.Action property you have to upgrade to .NET Framework 3.5 SP1.
9,667 views
ruslany on October 22nd 2008 in Other


(11 votes, average: 3.91 out of 5)
Wasted 2 hours before I found this solution. I can’t understand that this is not mentioned under “Using the module” in the http://learn.iis.net/page.aspx/460/using-url-rewrite-module/ page. I would guess that I am not the only one looking for this solution.
Thanks for the blog!
MadMan, thanks for feedback! I will make sure the articles at learn.iis.net will have this information.
respected sir,
I am new to ASP.NET. I am working on URL routing for last couple of weeks, although i have implemented routing on the static pages in my site, but i get stuck when i have to implement it on dynamic URL’s.
eg
If i have to generate url’s for the each user profile in my site, then the relative URL which needs to be generated should not contain the query string values such as the user id.
secondly
if i have two pages( home.aspx and profile.aspx) in my site in the same folder eg “logged”. The actual Url is mysite/logged/home.aspx.
i want the relative url to be mysite/index, which i have achieved using your code but now when i click on some other page and then when again i hit in the home tab the url displayed is mysite/logged/index which is wrong. The folders name is shown again in the url.
what should i do? hope you understand what i want to achieve.
regards
prabhjot
prabhjot,
Does your ASP.NET application use “~” symbols when constructing the URLs for the home tab? If yes, then this may be due to a bug in ASP.NET that we will fix by the time we ship RTW of the URL rewrite module.
More information about URL rewriting for ASP.NET web forms is available here: http://learn.iis.net/page.aspx/517/url-rewriting-for-aspnet-web-forms/
Awsom man…
Thanks a lot..I was lucky enough not to waste time that others have wasted.
Nilesh Patel.
Thanks a lot for this article.
I am working with .NET 2.0 and tried using the code as you have posted. It worked just fine.
Now I am wondering why you mentioned that it only works with .NET 3.5 SP1?? Is there something I am missing here?
As far as I know the Action property of the HtmlForm class did not exist until .NET Framework 3.5 SP1. If you are able to use this property then you must be using 3.5 SP1.
If you can’t update your Framework, you still can change tha form action using javascript.
Just put the HttpRequest.RawUrl value in any hidden input and then change de form action with something like this (using jquery because of the possibility to find tags by class):
$(document).ready(function(){
$(“.masterForm”).attr(“action”, $(“.rawUrl”).val());
});
not ellegant, but works.
Thanks so much – I had tried for days to get https redirection to work – the documentation is just vague enough to get me close but no results – your example worked the first try!
Thanks, that fixed my problem..
@Ruslany: FYI, I’m targeting .Net 2.0 and HtmlForm.Action property was present.
Thanks a lot! it was a great help………..
In our web page we use the following to remove default.aspx.
We use as above we get post action=”/default.aspx” and the post back is not working for the home page.
so we made the following change.
form1.Action = Request.RawUrl.Replace(“default.aspx”, “”)
so we made the modification,
We just moved our development boxes from IIS6 to IIS7 and were eager to replace our home grown url rewrite with IIS7 rewrite. .Net 3.5 SP1 is installed. The rewrite is working as expected, no issues, but we are unable to retrieve the original url and update the postback url. Both Request.RawUrl and Request.ServerVariables(“HTTP_X_ORIGINAL_URL”) contain the rewritten URL instead of the orginal URL. What did we miss?
TIA,
Bill
Bill, did you install .net 3.5 SP1 after URL rewrite? If yes, then you will need to re-install the URL Rewrite so that it applies the patch for the ASP.NET which fixes the Request.RawUrl behavior.
i have integrated url rewriting with 1.1 module with iis 7
Its working fine for static pages.
But i have some dynamic pages where page is depend on querystring.
i have to pass dynamic querysting from web.config in rule that i have defind
how should i achieved this.
any idea ?
@Jitendra: please send me the examples of input URL and what it should be rewritten to. You can use the contact form on this blog.
Hi ruslany,
I have Using URL Rewriting.Net Dll in my Project.
I have completed all Rewriting but I have One Issue, that is Postback Button click is not happaning in my Project after applying Rewriting rule.
I am Not able to set “form.Action = request.rawURL” because all the Pages is loading Dynamic and on the base of Querystring.
Example : – “MY Current Url: http://www.mydomain.com/home.aspx?pid=46”
I want to Rewriting as a http://www.mydomain.com/home.aspx/Calender or http://www.mydomain.com/home.aspx/Calender.aspx.
please provide me a proper solution for Button Click Event.
waiting for your reply
Regards
Hemant Tawri
@Hemant: I am not that familiar with URL Rewriting.NET. Have you tried using the IIS URL Rewrite Module instead? URL Rewrite 2.0 has a feature for changing the URL’s in the response HTML. You could use it to change the form.Action element in the response.