ASP.Net Web Forms, BASE HREF, and FORM ACTIONs

In some of the sites I develop, I use the BASE HREF tag. Unfortunately, doing this poses problems when you’re using Web forms in ASP.Net if your Web form is in a directory below the root directory.

If I create a page using Web forms in a subdirectory – as in http://somehostname/subfolder/webform.aspx – the FORM ACTION property of the form will point to http://somehostname/webform.aspx. Note how subfolder disappeared. This happens because of the BASE HREF tag. ASP.Net Web forms create the FORM ACTION without any reference to the physical path of the file – something that is not a problem unless you’re using BASE HREF.

Unfortunately, in a server-side FORM tag, you can not specify the ACTION property. But you can use JavaScript to change it. So, on each Web page where I need to adjust the FORM ACTION of a server-side ASP.Net Web form, I do something like this:

I use JavaScript to compile the complete path to the form, which includes the base URL (which I store in Application["basehref"]) followed by the subfolder (which I must type explicitly) followed by the current form action (which is taken dynamically within JavaScript). In four simple lines, I work around an ASP.Net shortcoming and continue to allow myself the benefit of using the BASE HREF tag!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.