This article is relevant to ASP.NET. Knowledge of HTML and ASP.Net is required.
I was working on a project where I had to implement URL rewriting. I was also working on a tool to update a site’s (let’s say a.com) content. The tool was hosted on another domain (b.com). I had to use images and CSS (everything else was in a database) lying at a.com onto b.com to preview the changes.
Let’s see what problems I faced in both the situations and how these were solved using Actionless
form. I have given similar examples, healing not the real situations.
Case 1: URL Rewriting
I was working on URL Rewriting where I had to work with hierarchical pages, something like below.
So the URLs should be:
- http://a.com/products/category2/product4.aspx and so on…
But the actual page exists at http://a.com/products/default.aspx.
There are some server controls on the page as well which raise page postback. For example, you have two dropdowns – one for categories and another for products. When you choose an item from categories dropdown it raises a postback to update the products dropdown to show relevant products.
By default, the action attribute of
form is set to the actual page and the path will be relative not absolute, “default.aspx” in this case.
So now, suppose you have opened http://a.com/products/category1/product1.aspx URL and you have picked an item from category. It will raise a postback event and look for default.aspx at http://a.com/products/category1 but its not there and the page will break. If you remove action attribute from
form element then it will always postback to the same page and the URL rewriting rules will take care of rest of the things.
Case 2: using one domain’s files on another domain/sub-domain
There might be cases when we have to use images and CSS lying on another domain. There are two options to achieve this – either use absolute path or use
base tab because the IMAGE URLs were saved in the database with the running text (I know I could search and replace them using regex but I still opted for
Now, when I have used
base tag, it will add a base URL to all the relative URLs and it includes the value of action attribute in form tag. Let’s name the domains, the images are at a.com and are being used on b.com. So when the postback event will be raised, it will find the action attribute file at a.com which isn’t there.
To overcome this situation, we can remove the action attribute so that it will always postback on the same page.
Unfortunately ASP.Net doesn’t give option to change or remove the value of action attribute directly. But a new form class can be implemented by inheriting System.Web.UI.HtmlControls.HtmlForm class. The implementation of Actionless form is given at this page – look for the section “Handling Postbacks”. You can also download the DLL from here.