ASP.Net Rewriting & PostBack Problem
ASP.Net Rewriting & PostBack Problem
Hello Friends,
I faced this problem when i was developing my site with url rewrite facility. When i click on any button it gives me error. So, i was googling and found solution. So i provide that here for you.
Every ASP.NET Web Form use postback event to maintain server-side control's state on the web page. Because of postback URL rewriting is somehow complecated for ASP.NET page. When we added one server side form control to the web page, ASP.Net will automatically render the response with HTML <form> tag, which contains an ACTION attribute pointing back to the page where the form control is placed. So, if we use URL rewrite for the page, then the ACTION attribute of the form tag will point back to the rewritten URL, not to the original URL which was requested from the browser.For Example :
Suppose we have a website which has one page called default.aspx. In this page we have one button control which makes postback. Now put http://localhost/urlrewritetest/default on the browser's address bar. As i used the rule to rewrite the URL from http://localhost/urlrewritetest/Default.aspx to http://localhost/urlrewritetest/default . After clicking on go button of browser it goes back and rewrite takes place and it converts to it's original url on server side. After the execution of the page it sends response to the web browser. Now check the source of the response. You will see that the page contains the <form> element, which looks like below:
<form name="form1" method="post" action="/urlrewritetest/Default.aspx" id="form1">
The ACTION attribute of the above tag contains the URL where the form data will be posted after clicking on the button. here it shows /urlrewritetest/Default.aspx on action attribute because the page sends response after rewritten the url. so it converts urlrewritetest/default to /urlrewritetest/Default.aspx . As we used the url rewrite method in this page, when we click on 'Click Me' button that post back to /urlrewritetest/Default.aspx and on server side it tries to url rewrite. Because of this, it gives error : The page not found. you can see on the browser's address bar. it shows /urlrewritetest/Default.aspx
To fix this issue, i have made some experiment and found one easy solution. You can use the property of the HtmlForm class called Action to set the postback URL to the one that was requested by browser before any rewriting happened. In ASP.NET you can obtain that URL by using HttpRequest.RawUrl property. So, to fix the postback URL for your web form when you use URL Rewrite Module, you have to add the following code to the page to work properly.Code :
protected void Page_Load(object sender, EventArgs e)
{
form1.Action = Request.RawUrl;
}
After this changes, put http://localhost/urlrewritetest/default and then click on 'Click Me' button you will see that the page correct result and browser's address bar shows the correct url.
If you view the HTML Source code of the response you will see that the <form> tag now contains the correct URL on it's action attributes.
Reference: http://blog.dotnetsquare.com/net/aspnet-rewriting-postback-problem/