Less code is fun: ASP.NET 3.5 SP1 removes need for control adapter when using URL rewriting

If you use URL rewriting in your ASP.NET apps you’re probably familiar with the workarounds required to get your rewritten URL to show up in the action attribute of the form on your pages (see ScottGu’s ‘Handling ASP.NET PostBacks with URL Rewriting’). Well, no more.

Included in the latest ASP.NET Service Pack is this gem (via Scott Hanselman):

HtmlForm.Action is now settable – Again, subtle, but very cool.  I like to use URL rewriting a lot and want my <form action=”"> to be a certain way. Now I can set it manually without fooling around with RegEx’s and messing with the whole response.

Woot! I deleted my FormRewriter.cs control adapter and .browser file then added this little snippet to my common BasePage (you can do this in either Page_Init or Page_Load on individual pages) and now my form action attributes reflect the original URL.

// header is created by Ionic’s ISAPI Rewrite Filter (IIRF)
string u = Request.Headers["X-Rewrite-Url"];
if (!string.IsNullOrEmpty(u))
{
   
Form.Action = u;
}

Be sure to check out the rest of Hanselman’s post for more details on what’s new in SP1.

Posted August 14th, 2008 6:23 PM
Read more posts about .NET, Tips.

View Comments
Link

  • joaocarlosleme
    Suddenly "Form" started to show on intellisense (magic), but that was not the problem. Request.Headers["X-Rewrite-Url"] is empty (null). so... I GOT IT TO WORK USING:

    Form.Action = "#";

    That worked for me. Small Problem: On postback WITHOUT AJAX it adds the # to the URL! Better idea? I tried Form.Action = ""; but it adds the default.aspx on it, maybe setting action="" on aspx page might work but since I'm using master pages I don't want to do that for all my pages. I also have a different solution http://urlrewriter.net/ on another site advogado trabalhista but it requires more files and code (I guess it was good before 3.5 came up.
  • joaocarlosleme
    Final solution for me:
    Form.Action = " ";
    No more # on URL
  • joaocarlosleme
    Hi there! I tried the code above and it didn't work. I guess the problem is the "Form." part, there ain't such a thing (do I need to add some directive "using System...)? or is it the reference to the form id "form1"?
    You can check at my site seguro automovel, as it is not working (at this time) when it do the postback.
  • Replace Form with the ID of your form (the one with the runat=server in your .aspx file). If your using a master page, you can't reference it directly from a content page codebehind. If you have a form with runat=server and no ID associated with it, just add an ID. If none of this works, post your problem with lots of sample code on stackoverflow.com
  • Fine, thanks!
  • CS
    Fine, but it seems to have as side effect thta the scottGu controlAdapter no more works with 3.5: I just installed it and trace in the AddAttribute method which should replace 'action' 's vlue, and their I get no more 'action'....some more massive modif behind this ???

    CS
blog comments powered by Disqus