Modify an Existing Meta Tag

I have this code to delete an existing meta tag that I don’t have access to, because it is in the DLL solution that it comes with, but basically, I want to delete the meta tag content that it belongs to our company content. The problem is that it does not find the meta tags, and I think this is because of how I set htmlHead = Page.Header ; I think I have something missing. But not sure. This code is in the virtual Page_Load in the base class.

  HtmlHead pHtml = Page.Header; for (int i = pHtml.Controls.Count - 1; i >= 0; i--) { if (pHtml.Controls[i] is HtmlMeta) { pMeta thisMetaTag = (HtmlMeta)pHtml.Controls[i]; if (thisMetaTag.Name == mName) { pHtml.Controls.RemoveAt(i); } } } 

I am not sure that I am giving the correct correction to the header, since this is in the virtual Page_Load in the base class. Also, most of this code was taken from (99%) from here. Code for deleting and replacing meta tags

Any help would be greatly appreciated

+4
source share
4 answers

This may be a problem with the order in which events occur. I created a new page in ASP.NET

  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="FormMail.WebForm1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> <meta http-equiv="keyword" name="testy" content="default content" /> </head> <body> <form id="form1" runat="server"> <div> </div> </form> </body> </html> 

Then I used:

  protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string mName = "testy"; HtmlHead pHtml = Page.Header; foreach (HtmlMeta metaTag in pHtml.Controls.OfType<HtmlMeta>()) { if (metaTag.Name.Equals(mName, StringComparison.CurrentCultureIgnoreCase)) { metaTag.Content = "Yeah!"; break; //You could keep looping to find other controls with the same name, but I'm exiting the loop } } //for (int i = pHtml.Controls.Count - 1; i >= 0; i--) //{ // if (pHtml.Controls[i] is HtmlMeta) // { // HtmlMeta thisMetaTag = (HtmlMeta)pHtml.Controls[i]; // if (thisMetaTag.Name == mName) // { // thisMetaTag.Content = "Yeah!"; // // pHtml.Controls.RemoveAt(i); // } // } //} } } 

When I look at the source, I see that the content of the meta tag has been changed. Now you can decide that during the loop the control does not exist (not yet added), and you add it, and then the inline code adds it.

EDIT. A moving code view for the PreRender incase controls is added after loading, but before rendering

  protected override void OnPreRender(EventArgs e) { if (!IsPostBack) { string mName = "testy"; HtmlHead pHtml = Page.Header; foreach (HtmlMeta metaTag in pHtml.Controls.OfType<HtmlMeta>()) { if (metaTag.Name.Equals(mName, StringComparison.CurrentCultureIgnoreCase)) { metaTag.Content = "Yeah!"; break; } } } base.OnPreRender(e); } 
+7
source

Although this is an old thread, I decided to use the LINQ-based approach to select the HtmlMeta control from the page control collection:

 HtmlMeta htmlMetaCtrl = (from ctrls in page.Header.Controls.OfType<HtmlMeta>() where ctrls.Name.Equals("keywords", StringComparison.CurrentCultureIgnoreCase) select ctrls).FirstOrDefault(); if (htmlMetaCtrl != null) htmlMetaCtrl.Content = metaContent; 

To change the meta tag dynamically, you can use the following general fuction:

  public class WebUtils { public static void SetPageMeta(string metaName, string metaContent, HttpContext httpContext = null) { if (string.IsNullOrWhiteSpace(metaName)) return; if (metaContent == null) throw new Exception("Dynamic Meta tag content can not be null. Pl pass a valid meta tag content"); if (httpContext == null) httpContext = HttpContext.Current; Page page = httpContext.Handler as Page; if (page != null) { HtmlMeta htmlMetaCtrl = (from ctrls in page.Header.Controls.OfType<HtmlMeta>() where ctrls.Name.Equals(metaName, StringComparison.CurrentCultureIgnoreCase) select ctrls).FirstOrDefault(); if (htmlMetaCtrl != null) page.Header.Controls.Remove(htmlMetaCtrl); htmlMetaCtrl = new HtmlMeta(); htmlMetaCtrl.HttpEquiv = metaName; htmlMetaCtrl.Name = metaName; htmlMetaCtrl.Content = metaContent; page.Header.Controls.Add(htmlMetaCtrl); } else { throw new Exception("Web page handler context could not be obtained"); } } } 

This can be called from OnPreRender from user controls (ascx controls) or Page_PreRender from a page (aspx):

  protected override void OnPreRender(EventArgs e) { if (!IsPostBack) { WebUtils.SetPageMeta("keywords", "Your keyword, keyword2, keyword3"); WebUtils.SetPageMeta("description", "Your page description goes here..."); } base.OnPreRender(e); } 

Hope this helps someone.

+2
source

Yes it is:

 HtmlHead pHtml = Page.Header; 

instead of this. Creating a new HTML header will not be entirely correct; instead, simply assigning a header will work much better. Just make sure the existing one:

 <head runat="server"></head> 

exists on the page, or on the main page, or on this page.

+1
source

If you want to remove the meta tag on the coding site, use the following code

 Dim meta4 As New HtmlMeta() meta4.Name = "AUTHOR" meta4.Content = "AUTHOR" Me.Page.Header.Controls.Remove(meta4) Dim meta5 As New HtmlMeta() meta5.Name = "DESCRIPTION" meta5.Content = "DESCRIPTION" Me.Page.Header.Controls.Remove(meta5) Dim meta6 As New HtmlMeta() meta6.Name = "KEYWORDS" meta6.Content = "KEYWORDS" Me.Page.Header.Controls.Remove(meta6) 
0
source

Source: https://habr.com/ru/post/1385402/


All Articles