2

Everything was working fine until I added a url rewrite that trims off the default.aspx from a page request. Once I added that all postbacks on default pages go back to the server, but do not fire the correct event.

The site is .net 2.0 (.net 4.0 is installed, but this site is not using it) The IIS server is version 7.5

Url rewrite rule:

<rule name="Default Document URL Rewrite" stopProcessing="true">
    <match url="(.*?)/?Default\.aspx$" />
    <action type="Redirect" url="{R:1}" />
</rule>

Sample markup code:

<form id="form1" runat="server">
    <asp:Button runat="server" ID="btnPostBack" Text="Post Back"
        OnClick="btnPostBack_Click" />
    <asp:Label runat="server" ID="lblDone" />
</form>

Sample code behind:

protected void btnPostBack_Click(object sender, EventArgs e)
{
    lblDone.Text = "Postback worked!";
}

NOTE: The action attribute of the form is not being rendered blank, so adding a line of code in the page_load to fill it explicitly with the Request.RawUrl, like suggested here: http://ruslany.net/2008/10/aspnet-postbacks-and-url-rewriting, did not work.

Neither did adding a Forms ControlAdapter like recommended here: Postback doesn't work with aspx page as Default Document.

Thank you in advance for your assistance!

Community
  • 1
  • 1
Lionscub
  • 21
  • 1

1 Answers1

0

Edit: The approach below seems to be working for some people as reported on some other websites. It's not working for me however. The URL Rewrite Rule always kicks in before Page_Load. :(

You could modify the URL for the Form Action by removing "default.aspx" from it.

This code snippet uses Regex to ignore case:

using System.Text.RegularExpressions;
protected void Page_Load(object sender, EventArgs e)
{
    Form.Action = Regex.Replace(Request.RawUrl.ToString(), "(.*)[dD]efault.aspx(.*)", "", RegexOptions.IgnoreCase);
}

The code snippet below allows you to ignore case by making everything lower case.

protected void Page_Load(object sender, EventArgs e)
{
    Form.Action = Request.RawUrl.ToLower().Replace("default.aspx", "");
}

I built upon Sahin Boydas' comment on http://ruslany.net/2008/10/aspnet-postbacks-and-url-rewriting/

It is good for SEO to have all your URLs either lower case or upper case because search engines count URLs in different cases as separate URLs. This affects the ranking of your URL. However, it is better to do that at the site level using something like IIS URL Rewrite instead of on a page level like the String.Replace snippet above.