ASP.NET automically HTML encoding HtmlMeta content

Last post 06-11-2009 5:21 AM by pnwright. 10 replies.

Sort Posts:

  • ASP.NET automically HTML encoding HtmlMeta content

    10-30-2008, 2:24 AM
    • Member
      point Member
    • Rastus7
    • Member since 10-30-2008, 5:55 AM
    • Sydney
    • Posts 7

    G’day all, 

     

    Recently I discovered that content placed into HtmlMeta control is automatically HTML encoded for me. How lovely of this to be taken care of for me automatically, however I do *not* want ‘&’ displaying in any of my meta tags, I want an actual ampersand, in fact, I *don’t want* any of my data to be messed with in any way unless explicitly specified.

     

    Here’s some (slightly sanitised) code from my master page presentation and code-behind files (snipped for brevity - assume Keywords = "Keywords Keywords Keywords & Keywords"):

    protected void SetMetaTag(string Title, string Keywords, string Description)
    {
        Page.Title = Title;
        oKeywords.Content = Keywords;
        oDescription.Content = Description;
    }
    <head id="oHead" runat="server">
    <title runat="server" id="oTitle"></title>
    <meta name="description" runat="server" id="oDescription" content="" />
    <meta name="keywords" runat="server" id="oKeywords" content="" />
    </head>

    And here (amazingly) is how it renders:

     

    <head id="ctl00_ctl00_ctl00_ctl00_oHead"><title>                TitleText

    </title><meta id="ctl00_ctl00_ctl00_ctl00_oDescription" name="description" content="Description" /><meta id="ctl00_ctl00_ctl00_ctl00_oKeywords" name="keywords" content="Keywords Keywords Keywords &amp; Keywords" /></head>

     

    As you can see, there’s nothing ground breaking there at all.

     

    This is beside the point, but I have no idea where the carriage returns are coming from, nor the bunching and jumbling of my meta tags, the main point here though is that an HTML encoded ampersand appears in the keywords!

     

    I will be awarding a shiny new donkey to the first person who can solve this issue for me without mentioning the following:

    1. Inheritance (I don't want to have to inherit from everything and rewrite it all in order for this issue to go away)

    2. Decoding the string myself (this doesn’t work anyway)

     

    Has anyone else experienced this issue?

     

    Thanks for your time!

    Adventures in recording and composition - http://www.mkiistudios.com/
  • Re: ASP.NET automically HTML encoding HtmlMeta content

    10-30-2008, 7:27 AM
    • Participant
      1,308 point Participant
    • sharpeffect
    • Member since 09-18-2008, 4:49 PM
    • Posts 219
    Hi please modify your code like this. 
    protected void SetMetaTag(string Title, string Keywords, string Description)
    {
        Page.Title = Server.HtmlDecode(Title);
        oKeywords.Content = Server.HtmlDecode(Keywords);
        oDescription.Content = Server.HtmlDecode(Description);
    }
    Hope this will work for you.Smile
  • Re: ASP.NET automically HTML encoding HtmlMeta content

    10-30-2008, 7:33 AM
    • Participant
      956 point Participant
    • sanketce
    • Member since 04-03-2006, 10:38 AM
    • Posts 195

    it will help you. 

    http://authors.aspalliance.com/aspxtreme/sys/web/httpserverutilityclasshtmldecode.aspx

     

    If this answer helps you, then please click “Mark as Answer”.
  • Re: ASP.NET automically HTML encoding HtmlMeta content

    10-30-2008, 6:13 PM
    • Member
      point Member
    • Rastus7
    • Member since 10-30-2008, 5:55 AM
    • Sydney
    • Posts 7

    Thanks for your suggestions, but as indicated in my original post:

     

    2. Decoding the string myself (this doesn’t work anyway)

     

    This means Server.HtmlDecode will not work, as the string is not encoded at all. I’ll try and explain it further.

     

    This string goes in like this: “text text & text text”

     

    But the HtmlMeta control renders it like this: “text text &amp; text text”

     

    As you can see, it appears as though the HtmlMeta control is marking it up for me, which confuses me somewhat. This would be like someone asking you for a phone number, and you adding a few of your own numbers to it before handing it back to them.

     

    Any other suggestions out there? I really appreciate the effort!

    Adventures in recording and composition - http://www.mkiistudios.com/
  • Re: ASP.NET automically HTML encoding HtmlMeta content

    10-30-2008, 6:19 PM
    • Member
      343 point Member
    • Natural Cause
    • Member since 05-23-2007, 1:30 AM
    • Australia
    • Posts 118

    Rastus7:
    This would be like someone asking you for a phone number, and you adding a few of your own numbers to it before handing it back to them.
     

    No, this would be like someone asking for your phone number, and you formatting the phone number into a proper format before handing it back to them.

  • Re: ASP.NET automically HTML encoding HtmlMeta content

    10-30-2008, 6:44 PM
    • Member
      point Member
    • Rastus7
    • Member since 10-30-2008, 5:55 AM
    • Sydney
    • Posts 7

    Natural Cause:
    No, this would be like someone asking for your phone number, and you formatting the phone number into a proper format before handing it back to them.

    Name one scenario where page keywords, or indeed the page title itself requires HTML encoding? Nobody searches for "B &amp; B" in a search engine at all, they search for "B & B", so I want my data to appear in the web control unaltered as it should.

    Generally when writing code that will interact with 3rd party data, you ensure that your code does not alter their data unless explicitly specified. Now, if the HtmlMeta control had a property on it name HtmlMeta.ContentEncoded rather than just HtmlMeta.Content, then this behaviour would be expected, and this whole thread would be unnecessary.

    Please refrain from posting in this thread unless you are going to post something constructive, which means that you are going to offer a genuine suggestion, or providing real information that will assist programmers that refer to this site. Apart from being wrong, your comment helps nobody.

     

    Adventures in recording and composition - http://www.mkiistudios.com/
  • Re: ASP.NET automically HTML encoding HtmlMeta content

    10-30-2008, 7:01 PM
    • Member
      343 point Member
    • Natural Cause
    • Member since 05-23-2007, 1:30 AM
    • Australia
    • Posts 118

    Sigh. If you think its going to affect searching that's your loss.

     Just add a literal control dynamically to the header.

     

            protected void Page_Load(object sender, EventArgs e)
            {
                AddMetaKeywords("B & M");
            }
    
            protected void AddMetaKeywords(string keywords)
            {
                //HtmlMeta metaKeyword = new HtmlMeta();
                //metaKeyword.Name = "keywords";
                //metaKeyword.Content = keywords;
    
                Literal metaKeyword = new Literal();
                metaKeyword.Mode = LiteralMode.PassThrough;
                metaKeyword.Text = string.Format("&lt;meta name=\"keywords\" content=\"{0}\" />", keywords);
    
                Page.Header.Controls.Add(metaKeyword);
            }
     
  • Re: ASP.NET automically HTML encoding HtmlMeta content

    10-30-2008, 10:13 PM
    • Member
      point Member
    • Rastus7
    • Member since 10-30-2008, 5:55 AM
    • Sydney
    • Posts 7

    Natural Cause:
    Sigh. If you think its going to affect searching that's your loss.

    Not really sure what you're sighing about, and yes, it certainly will affect searching. More to the point, if our clients want "B & B" in their meta tags, but don't get it, then we're not providing the service that they pay us for.

    Natural Cause:
    Just add a literal control dynamically to the header.

    Now this is more like it. I actually originally thought about going down this path, but then going one step further back than that, wondered why the control was rendering the data differently in the first place?

    Thanks for everyone’s suggestions, they have been somewhat useful tackling this issue.

    A while ago I needed to gain control of the <form> tag in order to govern the rendering of the action attribute. This was a relatively simple process (create a mapping in web.config, build the appropriate class to service the mapping) so I decided to employ this technique again, however this approach didn’t work.

     

    Here’s what my code looks like now, hopefully it's useful to someone.

     

    In the master page:

     

    <head id="oHead" runat="server">
    <title></title>
    </head>

     

    In the code-behind:

        protected void SetMetaTag(string Title, string Keywords, string Description)
        {
            Page.Title = Title;
    
            if ((Page.Header != null) && (Page.Header.Controls.Count > 0))
    
            {
                Page.Header.Controls.AddAt(1, new HtmlMeta("keywords", Keywords));
                Page.Header.Controls.AddAt(1, new HtmlMeta("description", Description));
    
            }
        }

    My HtmlMeta class:

        public class HtmlMeta : System.Web.UI.HtmlControls.HtmlMeta
        {
            #region Constructors
    
            public HtmlMeta(string Name, string Content)
            {
                base.Name = Name;
                base.Content = Content;
            }
    
            #endregion
    
            #region Constants
    
            #endregion
    
            #region Events
    
            #endregion
    
            #region Enumerations
    
            #endregion
    
            #region Fields
    
            #endregion
    
            #region Properties
    
            #endregion
    
            #region Methods
    
            protected override void Render(HtmlTextWriter HtmlTextWriter)
            {
                HtmlTextWriter.WriteLine();
                base.Render(HtmlTextWriter);
            }
    
            protected override void RenderAttributes(HtmlTextWriter HtmlTextWriter)
            {
                HtmlTextWriter.WriteAttribute("name", this.Name);
                base.Attributes.Remove("name");
    
                HtmlTextWriter.WriteAttribute("content", this.Content);
                base.Attributes.Remove("content");
            }
    
            #endregion
    
            #region Delegates
    
            #endregion
        }

    Interstingly enough, this.Content in RenderAttributes() is not HTML encoded! The encoding must take place somewhere else. Might be time to fire up Reflector and find out where.

     

    I would have preferred to use a straight mapping (as this would be the most elegant solution in my opinion), and I’m not sure why it wouldn’t load for me. If anyone has any idea as to how to get a tag mapping working with an HtmlMeta control, I’d be most interested! 

    Thanks for your time, TGIF etc!

    Adventures in recording and composition - http://www.mkiistudios.com/
  • Re: ASP.NET automically HTML encoding HtmlMeta content

    10-30-2008, 10:31 PM
    Answer
    • Member
      343 point Member
    • Natural Cause
    • Member since 05-23-2007, 1:30 AM
    • Australia
    • Posts 118

     

    Rastus7:
    Not really sure what you're sighing about, and yes, it certainly will affect searching. More to the point, if our clients want "B & B" in their meta tags, but don't get it, then we're not providing the service that they pay us for.

     I don't know about other search engines. But google treats & and &amp; the same. It does however treat &#038; different to & and &amp;.

    If the client is really anal about it then yeah, fix it. If the client is not then its perfectly acceptable for google since it accepts & and &amp; as the same thing.

     

    That is why i was sighing. I'm not gonna sit here and argue about it tho.

  • Re: ASP.NET automically HTML encoding HtmlMeta content

    10-30-2008, 10:44 PM
    • Member
      point Member
    • Rastus7
    • Member since 10-30-2008, 5:55 AM
    • Sydney
    • Posts 7
    Natural Cause:
    I don't know about other search engines. But google treats & and &amp; the same. It does however treat &#038; different to & and &amp;.
     That's interesting - do you have extra data on this at all? I would be most keen to check it out if you do! 
    Natural Cause:
    If the client is really anal about it then yeah, fix it. If the client is not then its perfectly acceptable for google since it accepts & and &amp; as the same thing.
     Absolutely! I try my best to give our clients exactly what they want, even if it means tearing my hair out over what (on the surface) appears to be relatively minor. Grr! 

    Thanks everyone again for your help – if anyone has a tag mapping solution, I’d love to see it!

    Adventures in recording and composition - http://www.mkiistudios.com/
  • Re: ASP.NET automically HTML encoding HtmlMeta content

    06-11-2009, 5:21 AM
    • Member
      2 point Member
    • pnwright
    • Member since 06-11-2009, 5:14 AM
    • Posts 1

    Hey all,

    I've stumbled across another problem that this automatic encoding produces where the page converts an opening <%= into &lt;%= causing an inline asp script to not run and become part of the actual meta content eg <meta content="&lt;%= DateTime.Now.ToString() %>" name="" /> etc. although it doesn't effect the closing asp script tag.

    Just thought i'd post to let you know

Page 1 of 1 (11 items)