Click here to Skip to main content
Click here to Skip to main content
Technical Blog

TIP: How to Handle Form Postbacks when Url Rewriting

, 15 Jul 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Url Rewriting is great and I love it a lot, you get to create nice readable (and memorable) urls without having to create hundred of files.  However, there can be issues.  For example, when you post back to a page where the url has been rewritten, it won’t be to the nice alias, it will be to the d

Url Rewriting is great and I love it a lot, you get to create nice readable (and memorable) urls without having to create hundred of files.  However, there can be issues. 

For example, when you post back to a page where the url has been rewritten, it won’t be to the nice alias, it will be to the direct page.aspx?id=blah location.  Which is a bit nasty.

To stop this, I make use of a Control Adapters which alters the rendering logic of the HtmlForm control on every ASP.Net page, to amend the action to be the nice alias.

   1: using System;
   2: using System.Collections.Generic; 
   3: using System.Linq;
   4: using System.Text; 
   5: using System.Web.UI.HtmlControls;
   6: using System.Web; 
   7:  
   8: namespace MartinOnDotNet.Web.ControlAdapters 
   9: {
  10:     /// <span class="code-SummaryComment"><summary> 
</span>  11:     /// Control adapter to ensure the form action persists the rewritten url
  12:     /// <span class="code-SummaryComment"></summary> 
</span>  13:     public class FormActionRewriterControlAdapter : System.Web.UI.Adapters.ControlAdapter
  14:     { 
  15:         /// <span class="code-SummaryComment"><summary>
</span>  16:         /// Overrides the <span class="code-SummaryComment"><see cref="M:System.Web.UI.Control.OnPreRender(System.EventArgs)"/> method for the associated control. 
</span>  17:         /// <span class="code-SummaryComment"></summary>
</span>  18:         /// <span class="code-SummaryComment"><param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param> 
</span>  19:         protected override void OnPreRender(EventArgs e)
  20:         { 
  21:             HtmlForm form = Control as HtmlForm;
  22:             if (form != null && HttpContext.Current != null) 
  23:             {
  24:                 form.Action = HttpContext.Current.Request.RawUrl; 
  25:             }
  26:             base.OnPreRender(e); 
  27:         }
  28:   
  29:         /// <span class="code-SummaryComment"><summary>
</span>  30:         /// Generates the target-specific markup for the control to which the control adapter is attached. 
  31:         /// <span class="code-SummaryComment"></summary>
</span>  32:         /// <span class="code-SummaryComment"><param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> to use to render the target-specific output.</param> 
</span>  33:         protected override void Render(System.Web.UI.HtmlTextWriter writer)
  34:         { 
  35:  
  36:             base.Render(new RewriteFormActionHtmlTextWriter(writer)); 
  37:         }
  38:     } 
  39: }

You'll notice that the control adapter needs a special HtmlTextWriter implementation to do the actual heavy lifting:

   1: using System;
   2: using System.Collections.Generic; 
   3: using System.Linq;
   4: using System.Text; 
   5: using System.Web;
   6: using System.Web.UI; 
   7: using System.IO;
   8:   
   9: namespace MartinOnDotNet.Web.ControlExtensions
  10: { 
  11:     /// <span class="code-SummaryComment"><summary>
</span>  12:     /// Specialised <span class="code-SummaryComment"><see ref="HtmlTextWriter" /> that handles populating the form action attribute appropriately for 
</span>  13:     /// url rewriting
  14:     /// <span class="code-SummaryComment"></summary> 
</span>  15:     public class RewriteFormActionHtmlTextWriter : HtmlTextWriter
  16:     { 
  17:         private bool _haveAlreadyWritten;
  18:   
  19:         /// <span class="code-SummaryComment"><summary>
</span>  20:         /// Initializes a new instance of the <span class="code-SummaryComment"><see cref="RewriteFormActionHtmlTextWriter"/> class. 
</span>  21:         /// <span class="code-SummaryComment"></summary>
</span>  22:         /// <span class="code-SummaryComment"><param name="writer">The writer.</param> 
</span>  23:         public RewriteFormActionHtmlTextWriter(TextWriter writer) : base(writer) { InnerWriter = writer; }
  24:         
  25:         /// <span class="code-SummaryComment"><summary>
</span>  26:         /// Initializes a new instance of the <span class="code-SummaryComment"><see cref="RewriteFormActionHtmlTextWriter"/> class. 
</span>  27:         /// <span class="code-SummaryComment"></summary>
</span>  28:         /// <span class="code-SummaryComment"><param name="writer">The writer.</param> 
</span>  29:         public RewriteFormActionHtmlTextWriter(HtmlTextWriter writer) : base(writer) { InnerWriter = writer.InnerWriter; }
  30:   
  31:         /// <span class="code-SummaryComment"><summary>
</span>  32:         /// Writes the specified markup attribute and value to the output stream, and, if specified, writes the value encoded. 
  33:         /// <span class="code-SummaryComment"></summary>
</span>  34:         /// <span class="code-SummaryComment"><param name="name">The markup attribute to write to the output stream.</param> 
</span>  35:         /// <span class="code-SummaryComment"><param name="value">The value assigned to the attribute.</param>
</span>  36:         /// <span class="code-SummaryComment"><param name="fEncode">true to encode the attribute and its assigned value; otherwise, false.</param> 
</span>  37:         public override void WriteAttribute(string name, string value, bool fEncode)
  38:         { 
  39:             if (string.Equals(name, "action", StringComparison.OrdinalIgnoreCase) && !_haveAlreadyWritten)
  40:             { 
  41:  
  42:                 value = HttpContext.Current.Request.RawUrl; 
  43:                 _haveAlreadyWritten = true;
  44:   
  45:             }
  46:             base.WriteAttribute(name, value, fEncode); 
  47:         }
  48:     } 
  49: }

This is then all registered in your web application with a simple FormAdapter.Browser file in the App_Browsers folder of your Web App.

   1: <browsers>
   2:   <browser refID="Default"> 
   3:     <controlAdapters>
   4:       <adapter controlType="System.Web.UI.HtmlControls.HtmlForm" 
   5:                adapterType="MartinOnDotNet.Web.ControlAdapters.FormActionRewriterControlAdapter" />
   6:              
   7:     </controlAdapters>
   8:   </browser> 
   9: </browsers>

These clases/config can now be reused across multiple projects quickly and easily.

Enjoy.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Martin Jarvis
Software Developer (Senior) Freestyle Interactive Ltd
United Kingdom United Kingdom
I'm a lead developer for Freestyle Interactive Ltd where we create many wonderful websites built on Microsofts ASP.Net and Ektron CMS.
 
I've been developing .Net applications (both Windows and Web) since 2002.
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 4 Pinmemberkrishcoding27-Aug-10 5:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 15 Jul 2010
Article Copyright 2010 by Martin Jarvis
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid