Can not figure out why but if I have button with PostBackUrl specified it
breaks UrlRewrite. All buttons stop working. Form submits but corresponded
event procedure never called. And it only happens in rewritten urls.
Do not understand why it a happens, hope some one from Microsoft would took
a look at it. I spent about a day trying to pinpoint a problem. Finaly did.
Here are simple steps to reproduce the problem
1. Create project UrlRewrite.
2. In Global.asax write following code (to rewrite every .aspx request to
default.aspx, we do not want to rewrite request for WebResource.axd )
void Application_Beg inRequest(Objec t sender, EventArgs e)
{
System.Web.Http Context ctx = System.Web.Http Context.Current ;
string sPath = ctx.Request.Pat h.ToLower();
//check that we have request for .aspx page
int iIndex = sPath.IndexOf(" .aspx");
if (iIndex == -1)
return;
ctx.Items["OriginalPa th"] = sPath;
ctx.RewritePath ("~/default.aspx", null, null);
return;
}
3. Make default.aspx
<%@ Page Language="C#"%>
<script runat="server">
protected override void OnInit(EventArg s e)
{
base.OnInit(e);
//Fix HtmlForm action for proper rewrite.
string sPath = (string)Context .Items["OriginalPa th"];
Context.Rewrite Path(sPath, "", "");
}
protected void Button1_Click(o bject sender, EventArgs e)
{
Button1.Text = "Success";
}
</script>
<html>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" OnClick="Button 1_Click"
Text="Try Me" />
<asp:Button ID="goBtn" runat="server" Text="Button"
PostBackUrl="/test.aspx"/>
</form>
</body>
</html>
by looking at this code you would say that when you hit Button1, Text =
"Try Me" will be replaced by "Success"
Start your project.
Url http://localhost/UrlRewrite/default.aspx works as expected
Url http://localhost/UrlRewrite/Test/default.aspx does not work, form
submits but button1_Click never called.
Remove PostBackUrl="/test.aspx" from goBtn and it starts working.
Thanks
George.