Tip/Trick: Url Rewriting with ASP.NET
page 4 of 6
by Scott Guthrie
Average Rating: 
Views (Total / Last 10 Days): 88460/ 73

Handling ASP.NET PostBacks with URL Rewriting

One gotcha that people often run into when using ASP.NET and Url-Rewriting has to-do with handling postback scenarios.  Specifically, when you place a <form runat="server"> control on a page, ASP.NET will automatically by default output the "action" attribute of the markup to point back to the page it is on.  The problem when using URL-Rewriting is that the URL that the <form> control renders is not the original URL of the request (for example: /products/books), but rather the re-written one (for example: /products.aspx?category=books).  This means that when you do a postback to the server, the URL will not be your nice clean one.

With ASP.NET 1.0 and 1.1, people often resorted to sub-classing the <form> control and created their own control that correctly output the action to use.  While this works, it ends up being a little messy - since it means you have to update all of your pages to use this alternate form control, and it can sometimes have problems with the Visual Studio WYSIWYG designer.

The good news is that with ASP.NET 2.0, there is a cleaner trick that you can use to rewrite the "action" attribute on the <form> control.  Specifically, you can take advantage of the new ASP.NET 2.0 Control Adapter extensibility architecture to customize the rendering of the <form> control, and override its "action" attribute value with a value you provide.  This doesn't require you to change any code in your .aspx pages.  Instead, just add a .browser file to your /app_browsers folder that registers a Control Adapter class to use to output the new "action" attribute:

Figure 1

You can see a sample implementation I created that shows how to implement a Form Control Adapter that works with URLRewriting in my sample here.  It works for both the Request.PathInfo and UrlRewriter.Net module approaches I used in Approach 1 and 2 above, and uses the Request.RawUrl property to retrieve the original, un-rewritten, URL to render.  With the ISAPIRewrite filter in Approach 4 you can retrieve the Request.ServerVariables["HTTP_X_REWRITE_URL"] value that the ISAPI filter uses to save the original URL instead.

My FormRewriter class implementation above should work for both standard ASP.NET and ASP.NET AJAX 1.0 pages (let me know if you run into any issues).

View Entire Article

User Comments

Title: Url Rewriting made Easy   
Name: ashish
Date: 2010-09-06 3:37:08 PM
Very useful article. I liked it. Thanks a lot.
Title: Url Rewriting with ASP.NET   
Name: http://apps.facebook.com/socialcontest
Date: 2009-08-21 5:18:20 AM

I am using url rewriting and Ajax, now wherever anything related to ajax is done and the page is posted back, the postback url changes(i.e. its adding the directory name in the url) coz of which iam getting the following javascript error and i can't move forward.

Error: Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500

I know it is an issue which lot of people are facing but how can i make url rewriting work with ajax..

The code which i have been using in Global is as follows:

string url = HttpContext.Current.Request.Url.AbsoluteUri;

if (url.Contains("404.aspx"))
string urlInfo405 = HttpContext.Current.Request.Url.AbsoluteUri;

string[] urlInfo404 = urlInfo405.Split(';');
string urlMain = urlInfo404[1];

string urlpath = urlMain.Replace("//", "/");

string[] urlInfo = urlpath.Split('?');
string path = urlInfo[0];
string[] currentPath = path.Split('/');

Context.Items.Add("Original_Path", currentPath[2] + ".aspx?cname=" + currentPath[3] + "&" + urlInfo[1]);
Context.RewritePath(currentPath[2] + ".aspx?cname=" + currentPath[3] + "&" + urlInfo[1],false);

I have even used Context.Items.Add which will fix my url for the entire post-response cycle.

Any guidance
Title: Nice article,   
Name: Baljeet Bhardwaj
Date: 2009-08-06 1:33:47 AM
nice article
Title: Found another working example   
Name: Samira
Date: 2009-04-07 2:32:22 AM
nice article but has few problems..i found another working article with example
Title: nike shoes   
Name: nike shoes
Date: 2009-04-06 11:37:24 PM
very good...
Title: Nice article, shame you copied it   
Name: Don't Copy Other People's Work
Date: 2009-01-25 4:39:02 PM
Nice article, shame you copied it
Title: not working   
Name: milap
Date: 2008-05-12 6:46:39 AM
hi please any example available for url rewriting then put here with code.
Title: not working   
Name: milap
Date: 2008-05-12 6:44:31 AM
Hi in this website download link is not working so please check this.
Title: Not running on IIS localhost   
Name: rajiv Sharma
Date: 2008-03-15 2:03:09 AM
Hi I am trying to use it, its working fien with ASP.NET Development Server, but it is not running on localhost (IIS) server, please help me. rajrunatserver@gmail.com
Title: QueryString   
Name: Hugo
Date: 2008-02-20 3:27:59 PM
Very good your article!!! Fantastic!

One more question... How can I do for this url: http://localhost:49195/UrlRewrite_HttpModule1/Products/Books.aspx?varB=1&varB=xxx

I want read querystring too...

Thanks. You can reply to me for hgoncalves@gmail.com
Title: Great Article   
Name: Adam Lock
Date: 2008-02-12 6:29:31 AM
Very information and well written - thanks
Title: in a shared hosting environment   
Name: Carlos Martinez
Date: 2007-12-04 9:55:29 AM
Hi, You said the first approach could work in a shared hosting environment. however, we always have to configure the IIS server when deployment. Or there is another option?? I have this problem now, and I have asked my hosting to allow me that.
Title: please help me config on server   
Name: bnet
Date: 2007-05-19 12:43:56 AM
please help me config on server
Title: can not run on server   
Name: bang
Date: 2007-05-18 9:40:10 PM
can not run on server
please help me
thank you

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-07-15 4:14:49 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search