Copied to clipboard

Flag this post as spam?

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


  • Stefan Kip 1614 posts 4131 karma points c-trib
    Sep 15, 2010 @ 16:55
    Stefan Kip
    0

    UrlRewriting, umbraco and the form action

    Hi people!

    Just noticed something; when UrlRewriting from '/' to '/?altTemplate=some-template', the form action attribute changes to the real url (with altTemplate query), not the RawUrl ('/').
    Thought this had nothing to do with Umbraco, so took a Google to find a solution and stumbled upon this blogpost by the Gu® himself: http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

    Found out there already is a ControlAdapter within umbraco, defined in the 'App_Browsers\Form.browser' file, pointing to 'umbraco.presentation.urlRewriterFormRewriterControlAdapter'.

    The code in this class does exactly what I don't want, because:

    • In the if-statement umbraco uses 'Context.Items["VirtualUrl"]', which is the real url (with altTemplate query)
    • In the else-statement umbraco appends the querystring too

    So my only option is to implement my own ControlAdapter, which I did. This one uses Request.RawUrl to set the action attribute of the form, which works fine.

    But, there must be a reason why umbraco doesn't use Request.RawUrl, right? Can anyone tell me what that reason is?

  • chappers 18 posts 36 karma points
    Oct 29, 2010 @ 11:02
    chappers
    0

    Hi there,

    I too am in the same situation - did you ever get an answer on this?  Did your own ControlAdapter (that uses RawUrl) cause any problems?

     

    Cheers

    Chris

  • chappers 18 posts 36 karma points
    Oct 29, 2010 @ 12:31
    chappers
    0

    Just to illustrate I've done this as a test...

     

    FormRewriterControlAdapterCustom.cs

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using umbraco.presentation;

    namespace MyNameSpaceXYZ
    {
        public class FormRewriterControlAdapterCustom : System.Web.UI.Adapters.ControlAdapter
        {
            protected override void Render(HtmlTextWriter writer)
            {
                base.Render(new UrlRewriterFormWriter(writer));
            }
        }

        public class UrlRewriterFormWriter : HtmlTextWriter
        {

            public UrlRewriterFormWriter(HtmlTextWriter writer)
                : base(writer)
            {
                base.InnerWriter = writer.InnerWriter;

            }

            public UrlRewriterFormWriter(System.IO.TextWriter writer)
                : base(writer)
            {

                base.InnerWriter = writer;

            }
            public override void WriteAttribute(string name, string value, bool fEncode)
            {
                if (name == "action")
                {
                    HttpContext Context;
                    Context = HttpContext.Current;
                    if (Context.Items["ActionAlreadyWritten"] == null)
                    {
                        string formAction = "";
                        if (Context.Items["VirtualUrl"] != null && !String.IsNullOrEmpty(Context.Items["VirtualUrl"].ToString()))
                        {
                            //formAction = Context.Items["VirtualUrl"].ToString();
                            formAction = Context.Request.RawUrl;
                        }
                        else
                        {
                            formAction = Context.Items[requestModule.ORIGINAL_URL_CXT_KEY].ToString();
                            if (!String.IsNullOrEmpty(Context.Request.Url.Query))
                            {
                                formAction += Context.Request.Url.Query;
                            }
                        }
                        value = formAction;
                        Context.Items["ActionAlreadyWritten"] = true;
                    }
                }
                base.WriteAttribute(name, value, fEncode);
            }
        }
    }

     

    Then in Form.browser:

     

    <browsers>
        <browser refID="Default">
            <controlAdapters>
                <!--<adapter controlType="System.Web.UI.HtmlControls.HtmlForm" adapterType="umbraco.presentation.urlRewriter.FormRewriterControlAdapter" />-->
                <adapter controlType="System.Web.UI.HtmlControls.HtmlForm" adapterType="MyNameSpaceXYZ.FormRewriterControlAdapterCustom" />
            </controlAdapters>
        </browser>
    </browsers>

     

    It has fixed the problem and so far can't see any knock on effects?

    Is this OK practise?

     

    Cheers

    Chris

     

     

     

  • Joey 20 posts 44 karma points
    Oct 27, 2011 @ 00:09
    Joey
    2

    Old post, but it helped me find out why it was impossible to set the form's action attribute in my code behind. Umbraco's custom control adapter was always kicking in and setting it. For any one else that runs into this you can simply add the following line in your code and it tricks their adapter into thinking it's already set the form's action attribute and allows you to set it to whatever you want in your own code. This is in umbraco 4.7.1.

    <code>

                Context.Items["ActionAlreadyWritten"] = true; // this tricks umbraco into not re-writing the form's action attribute

                Page.FindControlRecursive<HtmlForm>("rootForm").Action = "/Search.aspx";

    </code>

     

     

  • Stephen 767 posts 2273 karma points c-trib
    Oct 27, 2011 @ 19:48
    Stephen
    0

    Just wasted a few hours on this topic before finding your post. You rock!!

  • hansdesmedt 10 posts 31 karma points
    Aug 07, 2012 @ 15:54
    hansdesmedt
    0

    Ok thanks, this is exactly what I needed. 

Please Sign in or register to post replies

Write your reply to:

Draft