Click here to Skip to main content
12,451,232 members (52,681 online)
Click here to Skip to main content
Add your own
alternative version

Stats

9.2K views
8 bookmarked
Posted

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.

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>

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>

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>

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.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 4 Pin
krishcoding27-Aug-10 4:24
memberkrishcoding27-Aug-10 4:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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
Web02 | 2.8.160826.1 | Last Updated 15 Jul 2010
Article Copyright 2010 by Martin Jarvis
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid