C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Search engines, such as Google and Bing, require that webmasters have a tag that indicates that you own the site before you can look at their information. We add this tag with HtmlMeta.
Caution: Meta tags make page source larger. Sometimes it is best to avoid using them.
Example. In a master page setup, content pages do not have hard-coded HTML head sections. We need to dynamically add meta tags. ASP.NET provides a class called HtmlMeta in the System.Web namespace. We use that class in a Page_Load event.
Example that adds meta tag: C# protected void Page_Load(object sender, EventArgs e) { // This adds a metatag dynamically when run. AddMetaTag("verify-v1", "_NfoJpEJ/U+APc+dgVA1UV0YJqdAa+YKtP106LNZDl8="); } void AddMetaTag(string name, string content) { HtmlMeta meta = new HtmlMeta(); meta.Name = name; meta.Content = content; // Add the control to the HTML. Page.Header.Controls.Add(meta); }
This example uses a custom method. We use a convenience function called AddMetaTag to create a new HtmlMeta object. Its Name and Content properties are then set to the parameter values.
In the next steps, the elements are added to Page.Header. Finally, HtmlMeta is added to the Page.Header.Controls collection. This instructs ASP.NET to place the HTML metatag in the HEAD block, which I show an example of next.
Output of above code: HTML <!-- Head contents come before this... --> <meta name="verify-v1" content="_NfoJpEJ/U+APc+dgVA1UV0YJqdAa+YKtP106LNZDl8=" /> </head>
Summary. We used HtmlMeta in ASP.NET. HtmlMeta here makes fewer typos likely than hard-coding the HTML in various pages. Like HtmlTextWriter does for writing HTML, this approach for meta elements can reduce your risk of hard-to-find bugs.
Thus: HtmlMeta makes code longer to read, but clearer in its design. This reduces certain kinds of syntax errors.