Meta Tags Generator — C# source
Build the SEO head tags for any page — title, description, canonical, robots, Open Graph with smart fallbacks, Twitter card and theme-color — with live length warnings.
This is the C# implementation — the same logic the interactive tool runs, in a shareable, citable form.
// meta-tags-generator — build the ordered SEO head-tag set: title,
// description, canonical, robots, Open Graph (ogTitle/ogDescription/ogUrl
// falling back to the base fields, og:type website whenever the block is
// present), Twitter card, theme-color.
//
// Language: C# (.NET 8), standard library only
// CosmoDev polyglot showcase port of the `meta-tags-generator` tool.
// Ported from src/lib/meta-tags.ts — display source, part of CosmoDev's
// polyglot tool pages. Mirrors this dir's java.java structure 1:1
// (dotnet is not installed locally, so like the Java snippet this file is
// never compiled here; the other 13 drivers verify the shared vectors).
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace CosmoDev.MetaTagsGenerator;
/// <summary>The input field set; every field defaults to "" (all-optional shape).</summary>
public sealed class MetaTagsInput
{
public string Title = "";
public string Description = "";
public string CanonicalUrl = "";
public string Robots = "";
public string OgTitle = "";
public string OgDescription = "";
public string OgImage = "";
public string OgUrl = "";
public string TwitterCard = "";
public string TwitterSite = "";
public string ThemeColor = "";
}
/// <summary>The ordered tag list plus diagnostics.</summary>
public sealed class MetaTagsResult
{
public bool Ok;
public List<string> Errors = new();
public List<string> Warnings = new();
public List<string> Tags = new();
}
public static class MetaTagsGenerator
{
private static readonly Regex Hex = new("^#[0-9a-fA-F]{6}$", RegexOptions.Compiled);
private static string Trim(string s) => s?.Trim() ?? "";
/// <summary>Build the ordered head-tag set. Blank optional fields are
/// skipped; hard errors suppress emission entirely; warnings never
/// block emission.</summary>
public static MetaTagsResult BuildMetaTags(MetaTagsInput input)
{
var result = new MetaTagsResult();
var errors = new List<string>();
var title = Trim(input.Title);
var description = Trim(input.Description);
var canonical = Trim(input.CanonicalUrl);
var ogTitleIn = Trim(input.OgTitle);
var ogTitle = ogTitleIn.Length > 0 ? ogTitleIn : title;
var ogDescriptionIn = Trim(input.OgDescription);
var ogDescription = ogDescriptionIn.Length > 0 ? ogDescriptionIn : description;
var ogImage = Trim(input.OgImage);
var ogUrlIn = Trim(input.OgUrl);
var ogUrl = ogUrlIn.Length > 0 ? ogUrlIn : canonical;
var themeColor = Trim(input.ThemeColor);
if (title.Length == 0) errors.Add("Title is required.");
if (description.Length == 0) errors.Add("Description is required.");
if (ogImage.Length > 0 && !ogImage.StartsWith("http://") && !ogImage.StartsWith("https://"))
errors.Add("og:image must be an absolute http(s) URL.");
if (themeColor.Length > 0 && !Hex.IsMatch(themeColor))
errors.Add("theme-color must be a #rrggbb hex value.");
if (errors.Count > 0)
{
result.Ok = false;
result.Errors = errors;
return result;
}
if (title.Length > 60)
result.Warnings.Add($"Title is {title.Length} characters — search results truncate around 60.");
if (description.Length > 160)
result.Warnings.Add($"Description is {description.Length} characters — search results truncate around 160.");
var tags = new List<string>
{
$"<title>{title}</title>",
$"<meta name=\"description\" content=\"{description}\" />",
};
if (canonical.Length > 0) tags.Add($"<link rel=\"canonical\" href=\"{canonical}\" />");
var robots = Trim(input.Robots);
if (robots.Length > 0) tags.Add($"<meta name=\"robots\" content=\"{robots}\" />");
var hasOg = ogTitleIn.Length > 0 || ogDescriptionIn.Length > 0 || ogImage.Length > 0 || ogUrl.Length > 0;
if (hasOg)
{
tags.Add($"<meta property=\"og:title\" content=\"{ogTitle}\" />");
tags.Add($"<meta property=\"og:description\" content=\"{ogDescription}\" />");
if (ogImage.Length > 0) tags.Add($"<meta property=\"og:image\" content=\"{ogImage}\" />");
if (ogUrl.Length > 0) tags.Add($"<meta property=\"og:url\" content=\"{ogUrl}\" />");
tags.Add("<meta property=\"og:type\" content=\"website\" />");
}
if (input.TwitterCard.Length > 0)
tags.Add($"<meta name=\"twitter:card\" content=\"{input.TwitterCard}\" />");
var twitterSite = Trim(input.TwitterSite);
if (twitterSite.Length > 0) tags.Add($"<meta name=\"twitter:site\" content=\"{twitterSite}\" />");
if (themeColor.Length > 0) tags.Add($"<meta name=\"theme-color\" content=\"{themeColor}\" />");
result.Ok = true;
result.Tags = tags;
return result;
}
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →