A lightweight .NET library that automatically inserts <script> tags with the MIME type application/ld+json into the <head> of your HTML to help search engines better understand and index your content.
$ dotnet add package FoxLearn.AspNetCore.JsonLdFoxLearn.AspNetCore.JsonLd is a lightweight, easy-to-use .NET library that automatically injects JSON-LD structured data into your ASP.NET Core application. By adding JSON-LD <script> tags into your HTML , search engines like Google and Bing can better understand your content—resulting in enhanced SEO, rich snippets, and improved search visibility.
🔍 Perfect for blogs, e-commerce, and any ASP.NET Core app that needs to rank better on search engines with structured metadata.
(application/ld+json)<script> tag editingInstall via the .NET CLI:
dotnet add package FoxLearn.AspNetCore.JsonLd
Or via NuGet Package Manager
Search for FoxLearn.AspNetCore.JsonLd in Visual Studio’s NuGet UI.
1. Configure Services (Program.cs or Startup.cs)
Register JSON-LD services and set global schema metadata:
// Optional: Add default for each page
builder.Services.ConfigureJsonLd<WebSite>(options =>
{
options.Id = "https://foxlearn.com#website";
options.Url = new Uri("https://foxlearn.com");
options.Name = "FoxLearn";
options.Description = "Welcome to foxlearn.com! This site is a blog about everything that matters in the world of programming.";
options.InLanguage = "en-US";
options.PotentialAction = new SearchAction
{
Target = new Uri("https://foxlearn.com/post/search?q={search_term_string}"),
QueryInput = "required name=search_term_string"
};
});
builder.Services.AddJsonLd(); // Required to enable injection
2. Enable Middleware
Make sure to add UseJsonLd() in the app pipeline:
app.UseJsonLd(); // Enables automatic script injection
You can dynamically generate structured data using a helper service:
✅ Create a Helper Class: JsonLdGenerator
public class JsonLdGenerator
{
private const string Hosting = "https://foxlearn.com";
private readonly IJsonLdBuilder _jsonLdBuilder;
public JsonLdGenerator(IJsonLdBuilder jsonLdBuilder)
{
_jsonLdBuilder = jsonLdBuilder;
}
private Uri BuildUri(string path = "") => new Uri($"{Hosting}/{path}".TrimEnd('/'));
private Organization CreateOrganization() => new Organization
{
Name = "FoxLearn",
Url = BuildUri(),
Logo = new ImageObject()
{
Url = BuildUri("img/logo.png"),
},
Description = "Welcome to foxlearn.com! This site is a blog about everything that matters in the world of programming.",
SameAs = new[] { new Uri("https://x.com/tanhynh"), new Uri("https://www.youtube.com/foxlearn") },
ContactPoint = new[]
{
new ContactPoint
{
Name = "Tan Lee",
ContactType = "Customer Service",
Email = "mailto:contact@foxlearn.com"
}
}
};
private BreadcrumbList CreateBreadcrumb(string id, params (int pos, string name, string path)[] crumbs) => new BreadcrumbList
{
Id = id,
ItemListElement = crumbs.Select(c =>
{
var item = new ListItem
{
Position = c.pos,
Name = c.name,
Item = BuildUri(c.path)
};
return item;
}).ToArray()
};
public void Home()
{
_jsonLdBuilder.Add(new WebPage
{
Id = $"{Hosting}#webpage",
Url = BuildUri(),
Name = "Home",
InLanguage = "en-US",
IsPartOf = new WebSite
{
Id = $"{Hosting}#website"
}
});
_jsonLdBuilder.Add(CreateBreadcrumb($"{Hosting}#breadcrumb", (1, "Home", "")));
}
public void Contact()
{
_jsonLdBuilder.Add(new ContactPage
{
Id = $"{Hosting}/contact.html#contactpage",
Name = "Contact Us",
Url = BuildUri("contact.html"),
InLanguage = "en-US",
MainEntity = CreateOrganization(),
IsPartOf = new WebSite()
{
Id = $"{Hosting}#website"
}
});
_jsonLdBuilder.Add(CreateBreadcrumb($"{Hosting}/contact.html#breadcrumb",
(1, "Home", ""),
(2, "Contact Us", "contact.html")
));
}
public void Privacy()
{
_jsonLdBuilder.Add(new WebPage
{
Id = $"{Hosting}/privacy.html#webpage",
Name = "Privacy Policy",
Url = BuildUri("privacy.html"),
InLanguage = "en-US",
MainEntity = CreateOrganization(),
IsPartOf = new WebSite()
{
Id = $"{Hosting}#website"
}
});
_jsonLdBuilder.Add(CreateBreadcrumb($"{Hosting}/privacy.html#breadcrumb",
(1, "Home", ""),
(2, "Privacy Policy", "privacy.html")
));
}
public void About()
{
_jsonLdBuilder.Add(new AboutPage
{
Id = $"{Hosting}/about.html#aboutpage",
Name = "About Us",
Url = BuildUri("about.html"),
InLanguage = "en-US",
MainEntity = CreateOrganization(),
IsPartOf = new WebSite()
{
Id = $"{Hosting}#website"
}
});
_jsonLdBuilder.Add(CreateBreadcrumb($"{Hosting}/about.html#breadcrumb",
(1, "Home", ""),
(2, "About Us", "about.html")
));
}
public void Donate()
{
_jsonLdBuilder.Add(new WebPage
{
Id = $"{Hosting}/donate.html#webpage",
Name = "Donate",
Url = BuildUri("donate.html"),
InLanguage = "en-US",
MainEntity = new DonateAction
{
Name = "FoxLearn",
Target = BuildUri("donate.html"),
Description = "You're welcome to donate to foxlearn.com using the PayPal Donate form below. You can choose any amount you'd like.",
Recipient = CreateOrganization()
},
IsPartOf = new WebSite()
{
Id = $"{Hosting}#website"
}
});
_jsonLdBuilder.Add(CreateBreadcrumb($"{Hosting}/donate.html#breadcrumb",
(1, "Home", ""),
(2, "Donate", "donate.html")
));
}
public void HowTo()
{
_jsonLdBuilder.Add(new WebPage
{
Id = $"{Hosting}/howto.html#webpage",
Name = "How to",
Url = BuildUri("howto.html"),
InLanguage = "en-US",
MainEntity = CreateOrganization(),
IsPartOf = new WebSite()
{
Id = $"{Hosting}#website"
}
});
_jsonLdBuilder.Add(CreateBreadcrumb($"{Hosting}/howto.html#breadcrumb",
(1, "Home", ""),
(2, "How to", "howto.html")
));
}
public void Author()
{
var authorUrl = "author/tanlee";
_jsonLdBuilder.Add(new ProfilePage
{
Id = $"{Hosting}/author/tanlee#profilepage",
Name = "Tan Lee",
Url = BuildUri(authorUrl),
MainEntity = new Person
{
Name = "Tan Lee",
Url = BuildUri(authorUrl),
SameAs = new[] { new Uri("https://x.com/tanhynh"), new Uri("https://www.youtube.com/foxlearn") }
},
IsPartOf = new WebSite()
{
Id = $"{Hosting}#website"
}
});
_jsonLdBuilder.Add(CreateBreadcrumb($"{Hosting}/author/tanlee#breadcrumb",
(1, "Home", ""),
(2, "Author", authorUrl)
));
}
public void Search(string value)
{
var searchUrl = $"post/search?q={value}";
_jsonLdBuilder.Add(new SearchResultsPage
{
Id = $"{Hosting}/{searchUrl}#searchresultspage",
Name = "Search Results",
Url = BuildUri(searchUrl),
InLanguage = "en-US",
MainEntity = CreateOrganization(),
IsPartOf = new WebSite()
{
Id = $"{Hosting}#website"
}
});
_jsonLdBuilder.Add(CreateBreadcrumb($"{Hosting}/{searchUrl}#breadcrumb",
(1, "Home", ""),
(2, "Search", searchUrl)
));
}
public void Category(Category obj)
{
var categoryPath = $"{obj.UrlSlug}/page1.html";
_jsonLdBuilder.Add(new CollectionPage
{
Id = $"{Hosting}/{categoryPath}#collectionpage",
Name = obj.CategoryName,
Url = BuildUri(categoryPath),
InLanguage = "en-US",
MainEntity = CreateOrganization(),
IsPartOf = new WebSite()
{
Id = $"{Hosting}#website"
}
});
_jsonLdBuilder.Add(CreateBreadcrumb($"{Hosting}/{categoryPath}#breadcrumb",
(1, "Home", ""),
(2, obj.CategoryName!, categoryPath)
));
}
public void Detail(Post obj)
{
var postPath = $"{obj.CategoryUrlSlug}/{obj.PostUrlSlug}-{obj.PostId}.html";
var postUri = BuildUri(postPath);
var blogPosting = new BlogPosting
{
Id = $"{Hosting}/{postPath}#blogposting",
Headline = obj.Title,
Url = postUri,
DatePublished = obj.CreatedDate.ToUniversalTime(),
Description = obj.ShortDescription,
Author = new Person
{
Name = "Tan Lee",
Url = BuildUri("author/tanlee")
},
Publisher = CreateOrganization(),
IsPartOf = new WebSite()
{
Id = $"{Hosting}#website"
},
DateModified = obj.ModifiedDate?.ToUniversalTime(),
MainEntityOfPage = new WebPage
{
Id = $"{Hosting}/{postPath}"
}
};
if (!string.IsNullOrEmpty(obj.ImageUrl))
blogPosting.Image = new ImageObject { Url = BuildUri(obj.ImageUrl.TrimStart('/')) };
_jsonLdBuilder.Add(blogPosting);
_jsonLdBuilder.Add(CreateBreadcrumb($"{Hosting}/{postPath}#breadcrumb",
(1, "Home", ""),
(2, obj.CategoryName!, obj.CategoryUrlSlug!),
(3, obj.Title!, postPath)
));
}
}
✅ Use in Controller
public class HomeController : Controller
{
private readonly JsonLdGenerator _jsonLdGenerator;
public HomeController(JsonLdGenerator jsonLdGenerator)
{
_jsonLdGenerator = jsonLdGenerator;
}
public async Task<IActionResult> Index(int? page)
{
_jsonLdGenerator.Home();
var posts = await _postRepository.GetPagedPostsAsync(PostType.Article, page ?? 1);
return View(posts);
}
}
If you prefer injecting JSON-LD tags via Razor:
1. Enable the Tag Helper
In _ViewImports.cshtml:
@addTagHelper *, FoxLearn.AspNetCore.JsonLd
2. Inject in Layout
In your main layout (e.g., _Layout.cshtml):
<head>
...
<json-ld />
</head>
✅ Note: If you're using , you can remove app.UseJsonLd() from Program.cs.
Below is a sample output generated on a blog post page:
{
"@context": "https://schema.org",
"@graph": [
{
"@type": "WebSite",
"inLanguage": "en-US",
"@id": "https://foxlearn.com#website",
"url": "https://foxlearn.com",
"potentialAction": {
"@type": "SearchAction",
"query-input": "required name=search_term_string",
"target": "https://foxlearn.com/post/search?q={search_term_string}"
},
"name": "FoxLearn",
"description": "Welcome to foxlearn.com! This site is a blog about everything that matters in the world of programming."
},
{
"@type": "BreadcrumbList",
"itemListElement": [
{
"@type": "ListItem",
"position": 1,
"item": "https://foxlearn.com",
"name": "Home"
},
{
"@type": "ListItem",
"position": 2,
"item": "https://foxlearn.com/articles",
"name": "Articles"
},
{
"@type": "ListItem",
"position": 3,
"item": "https://foxlearn.com/articles/how-to-download-premium-bootstrap-themes-from-any-website-33.html",
"name": "Download Premium Bootstrap Themes from any website"
}
],
"@id": "https://foxlearn.com/articles/how-to-download-premium-bootstrap-themes-from-any-website-33.html#breadcrumb"
},
{
"@type": "BlogPosting",
"dateModified": "2024-11-26T15:17:00+00:00",
"isPartOf": {
"@type": "WebSite",
"@id": "https://foxlearn.com#website"
},
"headline": "Download Premium Bootstrap Themes from any website",
"author": {
"@type": "Person",
"url": "https://foxlearn.com/author/tanlee",
"name": "Tan Lee"
},
"datePublished": "2017-05-31T23:20:24+00:00",
"publisher": {
"@type": "Organization",
"contactPoint": [
{
"@type": "ContactPoint",
"email": "contact@foxlearn.com",
"contactType": "Customer Service",
"name": "Tan Lee"
}
],
"logo": {
"@type": "ImageObject",
"url": "https://foxlearn.com/img/logo.png"
},
"url": "https://foxlearn.com",
"sameAs": [
"https://x.com/tanhynh",
"https://www.youtube.com/foxlearn"
],
"name": "FoxLearn",
"description": "Welcome to foxlearn.com! This site is a blog about everything that matters in the world of programming."
},
"@id": "https://foxlearn.com/articles/how-to-download-premium-bootstrap-themes-from-any-website-33.html#blogposting",
"image": {
"@type": "ImageObject",
"url": "https://foxlearn.com/images/httrack-website-copier-a7e37240-3167.png"
},
"url": "https://foxlearn.com/articles/how-to-download-premium-bootstrap-themes-from-any-website-33.html",
"description": "To download premium Bootstrap themes/templates from websites like ThemeForest, TemplateMonster, WrapBootstrap, BootstrapMade, and ThemeWagon using HTTrack, follow these steps."
}
]
}
Using FoxLearn.AspNetCore.JsonLd can result in:
✅ Rich snippets (stars, FAQs, breadcrumbs) in Google
✅ Better content classification
✅ Enhanced visibility in Google Discover and News
✅ Higher click-through rates (CTR)
You can implement schemas for:
BlogPosting
AboutPage, ContactPage
BreadcrumbList
DonateAction
SearchResultsPage
ProfilePage
And more...
Create reusable builders to compose complex structured data for dynamic pages.
This project is licensed under the MIT License. Use it freely in personal or commercial projects.