PostBackUrl breaks url rewrite (Bug or unwanted feature).

G

George

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_BeginRequest(Object sender, EventArgs e)
{
System.Web.HttpContext ctx = System.Web.HttpContext.Current;
string sPath = ctx.Request.Path.ToLower();
//check that we have request for .aspx page
int iIndex = sPath.IndexOf(".aspx");
if (iIndex == -1)
return;

ctx.Items["OriginalPath"] = sPath;
ctx.RewritePath("~/default.aspx", null, null);
return;

}

3. Make default.aspx
<%@ Page Language="C#"%>
<script runat="server">
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
//Fix HtmlForm action for proper rewrite.
string sPath = (string)Context.Items["OriginalPath"];
Context.RewritePath(sPath, "", "");
}
protected void Button1_Click(object sender, EventArgs e)
{
Button1.Text = "Success";
}
</script>
<html>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" OnClick="Button1_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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top