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:

Postback1

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:

Postback2

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:

Postback3

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.

13,338 views

ruslany on October 22nd 2008 in Other

PoorFairAverageGoodExcellent (12 votes, average: 4.00 out of 5)

21 Responses to “ASP.NET postbacks and URL rewriting”

  1. Gravatar ImageMadMan responded on 28 Oct 2008 at 3:54 am #

    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!

  2. Gravatar Imageruslany responded on 28 Oct 2008 at 10:20 pm #

    MadMan, thanks for feedback! I will make sure the articles at learn.iis.net will have this information.

  3. Gravatar Imageprabhjot responded on 01 Nov 2008 at 4:05 am #

    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

  4. Gravatar Imageruslany responded on 03 Nov 2008 at 11:32 am #

    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.

  5. Gravatar Imageruslany responded on 11 Nov 2008 at 8:00 pm #

    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/

  6. Gravatar ImageNilesh Patel responded on 02 Apr 2009 at 10:56 am #

    Awsom man…

    Thanks a lot..I was lucky enough not to waste time that others have wasted.

    Nilesh Patel.

  7. Gravatar ImageExpertSoul responded on 13 Apr 2009 at 7:20 am #

    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?

  8. Gravatar Imageruslany responded on 13 Apr 2009 at 11:48 pm #

    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.

  9. Gravatar ImageJuliano Klein responded on 04 May 2009 at 6:16 pm #

    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.

  10. Gravatar ImageJeff K responded on 18 Aug 2009 at 1:07 pm #

    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!

  11. Gravatar ImageSteve responded on 09 Nov 2009 at 12:40 pm #

    Thanks, that fixed my problem..

    @Ruslany: FYI, I’m targeting .Net 2.0 and HtmlForm.Action property was present.

  12. Gravatar Imagepraveen responded on 17 Nov 2009 at 2:58 pm #

    Thanks a lot! it was a great help………..

  13. Gravatar ImageSahin boydas responded on 22 Nov 2009 at 3:03 pm #

    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,

  14. Gravatar ImageBill Hebb responded on 06 Jan 2010 at 6:30 am #

    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

  15. Gravatar Imageruslany responded on 06 Jan 2010 at 11:04 am #

    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.

  16. Gravatar Imagejitendra verma responded on 15 Feb 2010 at 10:45 pm #

    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 ?

  17. Gravatar Imageruslany responded on 16 Feb 2010 at 8:20 pm #

    @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.

  18. Gravatar ImageHemant responded on 28 Feb 2010 at 10:18 pm #

    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

  19. Gravatar Imageruslany responded on 02 Mar 2010 at 11:50 am #

    @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.

  20. Gravatar ImageWP responded on 22 Jun 2010 at 7:47 am #

    Hi Ruslany,

    We are mixing a lot of Asp.Net technologies in a large web environment. In approximately 6 months we will be moving over to IIS 7.5 (and Url Rewrite 2.0 off course), but for the moment we are still using IIS 6…and trying to incorporate a rewriting solution.

    The issues is that we came accross the famous “~ operator in ASP.NET Web server control is resolved incorrectly ” bug. You mentioned the Asp.Net kind-of-hotfix that takes place underneath the installation of Url Rewrite. A few questions:
    - Is this hotfix available for IIS 6 as well?
    - If not, is it possible to work around the issue? (keeping in mind that the usage of ResolveClientUrl and alike is widespread in the framework. And that we have a large codebase.)
    - Are there other strategies/alternatives (on IIS 6)

    Also note that the we are familiar with the various articles on the net (like scott mitchells old one, adapter strategies and the bug listed on connect with id 235385). In particular we fear sudden changed postback actions and alike…

    Regards and keep up the good work,

    WP

  21. Gravatar Imageruslany responded on 24 Jun 2010 at 5:18 pm #

    Hi WP:

    The hotfix for ASP.NET that is included with URL Rewrite is not applicable to IIS 6.

    I cannot think of a good workaround for that. One option may be to not use ~ operator and instead rely on absolute URL paths or to sue HTML <base> tag? But it will require substantial changes to the application code, which I am not sure you can afford.

Trackback URI | Comments RSS

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

XML Markup: If You want to add XML code to the comment please XML encode it first, otherwise the code will not show up.

Recently Published Articles