Setting the ASP.NET Form's Action Attribute, URL Rewriting

Click here to see a Video version of this article

If you have worked with URL Rewriting in ASP.NET, you would have figured out that it is pretty difficult to sustain since the ASP.NET Form tag's Action attribute cannot be overridden or manually set until you do some plumbing work.  In simple terms, you couldnt do a form1.Action = "Your Friendly URL" even though with URL mapping, the URL users see in the browser seems to be friendly.  What is the issue with the Action tag would be your question?  Well, there are couple of them.  When you make a postback from your friendly URL say...(yoursite/products which actually points to yoursite/products.aspx or something similar to it), after postback, the friendly URL changes to yoursite/products.aspx (the URL contained in the form's action tag.  Hence it kind of gives a discontinued structure for your website navigation.

Secondly, people complain about missing out on search engine rankings which, potentially can improve if there are more/friendly URLs as against a single URL with querystrings etc.,

While there are many work arounds, some of the significant ones below are:-

http://msdn.microsoft.com/en-us/library/ms972974.aspx

http://www.nikhilk.net/Tag-Mapping-Form-URL-Rewriting.aspx

http://www.codeproject.com/KB/aspnet/SmartFormControl.aspx

However, the good news is that, with .NET 3.5 SP1 this issue has been fixed and now, you can programmatically set the form's action tag to the desired friendly URL that you always wanted.

Note that this comes out only with .NET 3.5 SP1 and even if you are using Visual Studio 2008 and creating ASP.NET 3.5 websites, you cannot use this feature.  Hence, one more reason to install .NET 3.5 SP1 and VS 2008 SP1   

An example to make it simpler:-

Friendly URL : - www.yoursite.com/products 

Actual URL: - www.yoursite.com/products.aspx

URL mapping in Web.Config:

<add url="~/Products" mappedUrl="~/Products.aspx"/>

Setting the Form's Action attribute in the code-behind:-

form1.Action = "Products";

Cheers !!!

 

 

 

posted @ Sunday, August 31, 2008 11:41 PM

Print

Comments on this entry:

# re: Setting the ASP.NET Form's Action Attribute, URL Rewriting

Left by alex at 9/11/2008 11:34 PM
Gravatar
Is there a way to keep the URL mapping outside of web.config? Editing multiple web.config files does not sound appealing when you are operating on a server farm

# re: Setting the ASP.NET Form's Action Attribute, URL Rewriting

Left by Milwaukee Web Designer at 10/9/2008 9:53 AM
Gravatar
This is really great news! I began using URL rewritig with ASP.Net about 4 years ago and the form action attribute was always a particular thorn. I ended up creating a solution involved using a custom HtmlTextWriter that's called during Page.Render to set the action attribute as needed. Sort of a pain, but it worked. This new Form.Action capability is a welcome change. Thanks for sharing this.

# re: Setting the ASP.NET Form's Action Attribute, URL Rewriting

Left by Chad Grant at 12/19/2008 3:43 AM
Gravatar
been doing this since asp.net 1.1

just inherit HtmlForm, and override RenderAttributes

email me if you want the source

# re: Setting the ASP.NET Form's Action Attribute, URL Rewriting

Left by breast cancer staging at 4/16/2009 5:29 PM
Gravatar
Editing multiple web.config files does not sound appealing when you are operating on a server farm

# re: Setting the ASP.NET Form's Action Attribute, URL Rewriting

Left by Oğuz at 10/1/2009 8:43 AM
Gravatar
thx for sharing, Fortunately They have fixed it sp1 :)

# re: Setting the ASP.NET Form's Action Attribute, URL Rewriting

Left by Internet Marketing Success Stori at 10/8/2009 6:02 AM
Gravatar
Great post! My friend runs a small business we do internet marketing for companies and so a lot of our e-newsletter, blog and seminar content revolve around for promotion. Thanks!!!

# re: Setting the ASP.NET Form's Action Attribute, URL Rewriting

Left by Jeff Paul Forum at 10/10/2009 5:11 AM
Gravatar
Great work! What is discuss here is one side of the coin, what about social media, isnt it a challenge to google

# re: Setting the ASP.NET Form's Action Attribute, URL Rewriting

Left by Internet Business Review at 10/10/2009 7:00 AM
Gravatar
Really Nice Post. More and more businesses are indulge with online presence, it is costly if you find a wrong company to do your job

# re: Setting the ASP.NET Form's Action Attribute, URL Rewriting

Left by Michelle Boudreau at 10/12/2009 12:26 PM
Gravatar

I like your post. No matter what amount of debt a person has, willingness to retire is the first step

# re: Setting the ASP.NET Form's Action Attribute, URL Rewriting

Left by Debt Rescue Relief at 10/13/2009 6:11 PM
Gravatar
It’s really amazing. Debt can be rescued by reducing expenses not by increase income

# re: Setting the ASP.NET Form's Action Attribute, URL Rewriting

Left by John Beck Property Vault at 10/16/2009 7:48 AM
Gravatar
I believe the real estate market has much to offer, like what is being offered by John Beck in foreclosure properties

# re: Setting the ASP.NET Form's Action Attribute, URL Rewriting

Left by Tax Foreclosure at 10/17/2009 7:05 AM
Gravatar
Foreclosure properties are in high demand as the lenders are in troble and there are many tax foreclosure properties available

# re: Setting the ASP.NET Form's Action Attribute, URL Rewriting

Left by Tax Foreclosed Properties at 10/18/2009 5:12 AM
Gravatar
Very Informative blog. This is true, in real estate investment foreclosure properties under tax lien is big investment

# re: Setting the ASP.NET Form's Action Attribute, URL Rewriting

Left by John Beck Tax Sales at 10/19/2009 6:56 AM
Gravatar
Getting to know about the real estate investment starts with an understanding to worth of house / average worth of neighbours

# re: Setting the ASP.NET Form's Action Attribute, URL Rewriting

Left by Free and Clear System at 10/20/2009 5:19 PM
Gravatar
Nice post. The profit gain from real estate is enormous but on other side those who really wants to buy a house gets hurt

# re: Setting the ASP.NET Form's Action Attribute, URL Rewriting

Left by Conciant at 10/23/2009 12:43 PM
Gravatar

I like your post, it's a really good stuff, keep it up! Office Conceirge has another benefit that it is economic too

# re: Setting the ASP.NET Form's Action Attribute, URL Rewriting

Left by Wealthy Women Debts at 11/19/2009 8:36 AM
Gravatar
Its very intresting and Informative stuff. Women finds it difficult to manage their cost, save $1 a day and you can see the difference it makes

# re: Setting the ASP.NET Form's Action Attribute, URL Rewriting

Left by Agenzia Web Marketing at 11/25/2009 5:44 PM
Gravatar
Great post.It really contains valuable information.Thanks for sharing.

# re: Setting the ASP.NET Form's Action Attribute, URL Rewriting

Left by Bride Dresses at 12/27/2009 1:06 AM
Gravatar
Render to set the action attribute as needed. Sort of a pain, but it worked. This new Form.Action capability is a welcome change. Thanks for sharing this.

# re: Setting the ASP.NET Form's Action Attribute, URL Rewriting

Left by Volker at 12/30/2009 9:56 AM
Gravatar
Ein Kompliment für diese tolle Seite.
Eigentlich bin ich durch Zufall darauf gestoßen und dachte mir,
einen netten Eintrag und viele Grüße zu hinterlassen.
_____ \\\!!!///____
______( ô ô )_____
___ooO-(_)-Ooo___
Vielleicht schauen sie mal auf meiner Homepage vorbei!

# re: Setting the ASP.NET Form's Action Attribute, URL Rewriting

Left by Dave at 1/3/2010 8:45 PM
Gravatar
:) Thanks for sharing.

# Lob

Left by Birgit Gosch at 1/21/2010 7:20 AM
Gravatar
Hallo Leute,
eine wirklich gelungene Homepage!!! Weiter so. Viele Grüße vom Schönberger Strand an der Ostsee.
_____ \\\!!!///____
______( ô ô )_____
___ooO-(_)-Ooo___

# re: Setting the ASP.NET Form's Action Attribute, URL Rewriting

Left by reiner at 2/1/2010 7:26 PM
Gravatar
Viele Grüße aus dem verschneiten Greetsiel

Your comment:



 (will not be displayed)


 
 
 
 
 

Live Comment Preview:

 
«February»
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
28123456
78910111213