Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Andreas Paulsson 9 posts 29 karma points
    Dec 17, 2009 @ 09:34
    Andreas Paulsson
    0

    Problems with url rewriting after user control postback

    Hello all,

     

    I am using url rewiriting to have nice looking URL in a campaign part of a new site:

    I would like the URL /campaign/campaignname.aspx be rewritten to  /campaign-aspx?campaign=campaignname .

    I do this by adding

    <add name="campaignrewrite"
                  virtualUrl="^~/campaign/(.*).aspx"
                  rewriteUrlParameter="ExcludeFromClientQueryString"
                  destinationUrl="~/campaign.aspx?campaign=$1"
                  ignoreCase="true" />

    in /config/UrlRewriting.config (I am using umbraco 4.0.2.1).

    The rewriting works correctly, but in the generated HTML for the page /campaign/Testcampaign.aspx , I see that the form is defined as follows:

    <form id="aspnetForm" action="/campaign.aspx?campaign=Testcampaign" method="post">

    which means that after a postback (caused by a user control in the page), the URL in the address bar has changed to

    /campaign.aspx?campaign=Testcampaign.


    Everything works, so this is not a major problem, but to my eyes, it looks weird since I do think that the URL should stay the same since the URL rewriting module should modify the action URL on the form. I have used the urlrewriting.net module in non-umbraco sites without problems.

    Does anyone have any ideas?

     

    Regards,

      Andreas Paulsson

  • Dean 55 posts 98 karma points
    Sep 01, 2010 @ 05:23
    Dean
    0

    Hello,

    I'm new to umbraco, and have stumbled across exaclty the same issue. I posted about it here: http://our.umbraco.org/forum/developers/api-questions/12095-Url-Rewriting-tilda-no-longer-finding-site-root

    I'm having an additional issue where paths are being resolved incorrectly on the re-written pages too. But I assume when one is fixed the other will be sorted too.

    I know it has been a while, but i'm clutching at straws. Did you manage to find a solution to this problem?

    Regards

    Dean

  • Kevin Blake 23 posts 45 karma points
    Mar 31, 2011 @ 13:03
    Kevin Blake
    0

    I'm still looking for a solution to this, too...

    In a traditional asp.net app, I'd have an overridden form control (pre 3.5 SP1), such as:

     public class UrlRewriteForm : System.Web.UI.HtmlControls.HtmlForm
        {
            protected override void RenderAttributes(HtmlTextWriter writer)
            {
                writer.WriteAttribute("name", this.Name);base.Attributes.Remove("name");
                writer.WriteAttribute("method", this.Method);
                base.Attributes.Remove("method");
                this.Attributes.Render(writer);
                base.Attributes.Remove("action");
                writer.WriteAttribute("action", HttpUtility.HtmlEncode(Context.Request.RawUrl));
                writer.WriteAttribute("onsubmit", "if (typeof(WebForm_OnSubmit) == 'function') return WebForm_OnSubmit();");
                base.Attributes.Remove("onsubmit");
                if (base.ID != null)
                    writer.WriteAttribute("id", base.ClientID);
            }
        }

    or with > 3.5 SP1, as simple as

    Form1.Action = Context.Request.RawUrl

    But that seems to be overridden somewhere in Umbraco 4.6... Is there something obvious I'm missing?

    Many thanks,

    Kev.

  • Kevin Blake 23 posts 45 karma points
    Mar 31, 2011 @ 13:23
    Kevin Blake
    0

    Actually, I've just found a solution to this, to remove the action tag altogether - then nothing seems to be changed further down the lifecycle... Obviously this will only be workable if you only ever have pages that postback to themselves

     

     public class UrlRewriteForm : System.Web.UI.HtmlControls.HtmlForm
       
    {
           
    protected override void RenderAttributes(HtmlTextWriter writer)
           
    {
                writer
    .WriteAttribute("name", this.Name);base.Attributes.Remove("name");
                writer
    .WriteAttribute("method", this.Method);
               
    base.Attributes.Remove("method");
               
    this.Attributes.Render(writer);
               
    base.Attributes.Remove("action");
                writer.WriteAttribute("onsubmit", "if (typeof(WebForm_OnSubmit) == 'function') return WebForm_OnSubmit();");
               
    base.Attributes.Remove("onsubmit");
               
    if (base.ID != null)
                    writer
    .WriteAttribute("id", base.ClientID);
           
    }
       
    }

     

    In the web.config

     

    <controls>
    <add tagPrefix="prefix" namespace="<namespace>" assembly="<assembly>"/>
    ...
    </controls>
    And on the masterpage
    <prefix:UrlRewriteForm runat="server" ID="Form1">
    ...
    </prefix:UrlRewriteForm>

     

    Now my URLs stay rewritten.  Happy days :)


     

  • Andrew Lansdowne 43 posts 124 karma points
    Jul 13, 2011 @ 12:41
    Andrew Lansdowne
    0

    Thanks, that worked.

    I read somewhere that it is dodgy to remove the action attribute as the behaviour is unspecified in the html spec so some browsers might break if you leave it blank, so I made it work like first example where it uses the RawUrl. I also changed it a little for .NET 4 so it wouldn't mess with any other stuff that the .NET form control does (the RenderAttributes function does quite a lot if you look at it in reflector):

        public class UrlRewriteForm : System.Web.UI.HtmlControls.HtmlForm
        {
            protected override void RenderAttributes(HtmlTextWriter writer)
            {
                // Render attributes normally to a string
                StringBuilder sb = new StringBuilder();
                StringWriter tw = new StringWriter(sb);
                HtmlTextWriter hw = new HtmlTextWriter(tw);
                base.RenderAttributes(hw);
                string s = sb.ToString();
    
                // Replace action with custom action
                string newAction = "action=\"" + HttpUtility.HtmlEncode(Context.Request.RawUrl) + "\"";
                s = System.Text.RegularExpressions.Regex.Replace(s, "action=\".*?\"", newAction);
    
                // Write the new attributes
                writer.Write(s);
            }
        }
    

    It's not particularly great, but it works OK for me :-)

Please Sign in or register to post replies

Write your reply to:

Draft