PostBackUrl breaks url rewrite (Bug or unwanted feature).

Discussion in 'ASP .Net' started by George, Jun 14, 2008.

  1. George

    George Guest

    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.
    George, Jun 14, 2008
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VA==?=

    Rewrite asp url as aspx url?

    =?Utf-8?B?VA==?=, Jul 22, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    717
    gerry
    Jul 24, 2005
  2. Daves
    Replies:
    0
    Views:
    398
    Daves
    Mar 27, 2006
  3. cmay
    Replies:
    0
    Views:
    330
  4. Fabio Mastria
    Replies:
    2
    Views:
    2,553
  5. Martin
    Replies:
    0
    Views:
    1,217
    Martin
    Apr 28, 2010
Loading...

Share This Page