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 !!!

 

 

 

Print | posted on Sunday, August 31, 2008 11:41 PM

Comments on this post

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

Requesting 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
Left by alex on Sep 11, 2008 11:34 PM

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

Requesting 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.
Left by Milwaukee Web Designer on Oct 09, 2008 9:53 AM

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

Requesting Gravatar...
been doing this since asp.net 1.1

just inherit HtmlForm, and override RenderAttributes

email me if you want the source
Left by Chad Grant on Dec 19, 2008 3:43 AM

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

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

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

Requesting Gravatar...
thx for sharing, Fortunately They have fixed it sp1 :)
Left by Oğuz on Oct 01, 2009 8:43 AM

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

Requesting 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!!!
Left by Internet Marketing Success Stori on Oct 08, 2009 6:02 AM

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

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

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

Requesting 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
Left by Internet Business Review on Oct 10, 2009 7:00 AM

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

Requesting Gravatar...

I like your post. No matter what amount of debt a person has, willingness to retire is the first step
Left by Michelle Boudreau on Oct 12, 2009 12:26 PM

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

Requesting Gravatar...
It’s really amazing. Debt can be rescued by reducing expenses not by increase income
Left by Debt Rescue Relief on Oct 13, 2009 6:11 PM

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

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

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

Requesting Gravatar...
Foreclosure properties are in high demand as the lenders are in troble and there are many tax foreclosure properties available
Left by Tax Foreclosure on Oct 17, 2009 7:05 AM

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

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

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

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

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

Requesting 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
Left by Free and Clear System on Oct 20, 2009 5:19 PM

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

Requesting Gravatar...

I like your post, it's a really good stuff, keep it up! Office Conceirge has another benefit that it is economic too
Left by Conciant on Oct 23, 2009 12:43 PM

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

Requesting 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
Left by Wealthy Women Debts on Nov 19, 2009 8:36 AM

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

Requesting Gravatar...
Great post.It really contains valuable information.Thanks for sharing.
Left by Agenzia Web Marketing on Nov 25, 2009 5:44 PM

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

Requesting 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.
Left by Bride Dresses on Dec 27, 2009 1:06 AM

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

Requesting 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!
Left by Volker on Dec 30, 2009 9:56 AM

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

Requesting Gravatar...
:) Thanks for sharing.
Left by Dave on Jan 03, 2010 8:45 PM

# Lob

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

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

Requesting Gravatar...
Viele Grüße aus dem verschneiten Greetsiel
Left by reiner on Feb 01, 2010 7:26 PM

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

Requesting Gravatar...
A real deal?
Left by Kostenloses Konto on Feb 10, 2010 8:47 AM

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

Requesting Gravatar...
It seems so....
Left by Tagesgeldvergleich on Feb 10, 2010 8:48 AM

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

Requesting Gravatar...
Uh men....
Left by Geldanlagen on Feb 10, 2010 8:48 AM

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

Requesting Gravatar...
The same....
Left by Kreditvergleich on Feb 10, 2010 8:49 AM

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

Requesting Gravatar...
Hi Kerry ;)
Left by Girokonto Vergleich on Feb 15, 2010 4:51 AM

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

Requesting Gravatar...
Danke für die vielen Infos, hätte nicht gedacht, mal so gut beraten zu werden...


Gruß

Günter
Left by Günter Hogrefe on Mar 16, 2010 4:36 AM

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

Requesting Gravatar...
This is really great news! I started using URLs with ASP.Net rewritig about 4 years ago and the form action attribute is always a particular thorn. I finally created a solution that involved using a custom HtmlTextWriter called for Page.Render to set the action attribute is required. Sort of pain, but it worked. Form.Action new capabilities are welcome changes. Thank you for sharing this.
Left by free xbox live on Apr 19, 2010 4:57 PM

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

Requesting Gravatar...
Viele Grüße aus dem schönen Fischerdorf Greetsiel
Left by reiner on May 04, 2010 4:56 PM

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

Requesting Gravatar...
ALIN ZAYIFLAYIN
Left by meksika biberi zayıflama hapı on May 31, 2010 11:24 PM

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

Requesting Gravatar...
Editing multiple web.config files does not sound appealing when you are operating on a server farm
Left by lovely on Jul 11, 2010 6:40 PM

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

Requesting Gravatar...
Danke für die Informationen. Kenne mich mit URL rewrite an sich ganz gut aus, habe aber auf Ihrer Seite doch einiges dazugelernt...
Left by preisvergleiche on Aug 01, 2010 12:57 AM

# danke

Requesting Gravatar...
gute infos. schoene gruesse aus der verregneten schweiz...
Left by seitensprung on Oct 17, 2010 12:03 AM

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

Requesting Gravatar...
aus, habe aber auf Ihrer Seite doch einiges dazugelernt...
Left by توبيكات 2010 on Oct 25, 2010 9:24 PM

# Grüße vom Schönberger-Strand

Requesting Gravatar...
Viele Grüße vom Schönberger-Strand und der Ostsee
Gaby
Left by Schönberger-Strand on Nov 15, 2010 9:39 PM

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

Requesting Gravatar...
...von mir auch...
Left by Schönberger Strand on Nov 28, 2010 5:21 PM

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

Requesting Gravatar...
Great example. I was having a hard time grasping the concept until you put that in there. I'm just a visual learner.
Left by James on Dec 16, 2010 4:33 AM

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

Requesting Gravatar...
Happy New Year
Gaby
Left by Kalifornien Ostsee on Jan 17, 2011 3:47 PM

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

Requesting Gravatar...
Alles Gute zum Neuen Jahr
Micha
Left by Laboe Ostsee on Jan 17, 2011 3:48 PM

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

Requesting Gravatar...
Super, vielen Dank für diese Infos!
Left by Hüpfburg mieten Wiesbaden on Jan 28, 2011 11:58 PM

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

Requesting Gravatar...
Schöne Grüße aus dem kalten Deutschland
Left by Kredite Ratgeber on Mar 09, 2011 11:21 PM

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

Requesting Gravatar...
There is some a lot garabage on blogs nowadays that it is hard to discover the true diamonds. Glad i just discovered 1. thanks:) latest news today || News
Left by umair malikg on Nov 16, 2011 8:37 PM

# the math tutor

Requesting Gravatar...
Interesting article. Thanks for the info. Regards. Online math tutoring
Left by Online Math Tutoring on Dec 06, 2011 8:47 PM

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

Requesting Gravatar...
This is really nice and interesting blog.I m glad to know.
Left by Fare soldi on Feb 27, 2012 3:45 PM

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

Requesting Gravatar...
I also use UrlMapping sometimes in smaller projects, and have had some success with this line added to my Site.Master.cs Page_Load method:

form1.Action = "./";

Left by Ryan Hathaway on Jan 02, 2014 10:57 PM

Your comment:

 (will show your gravatar)