TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

ASP.NET HtmlMeta Element

This example C# code uses HtmlMeta to add meta tags to HTML.

HtmlMeta. An HtmlMeta can add meta tags to 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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf