Building Dynamic Structured Data with JSON-LD in ASP.NET Core
By Tan Lee Published on May 28, 2025 62
In this article, we’ll walk through how to dynamically generate JSON-LD structured data in an ASP.NET Core application using the FoxLearn.JsonLd.AspNetCore library.
FoxLearn.JsonLd.AspNetCore is a lightweight .NET library that simplifies the process of adding JSON-LD structured data to your ASP.NET Core applications. It automatically injects JSON-LD in the <head>
section of your HTML without needing any manual HTML edits.
The first step is to install the FoxLearn.JsonLd.AspNetCore NuGet package. You can install it in your project via the .NET CLI or using Visual Studio's NuGet Package Manager.
Via the .NET CLI:
Open your terminal and run the following command:
dotnet add package FoxLearn.JsonLd.AspNetCore
Via the NuGet UI in Visual Studio:
Right-click on your project and select Manage NuGet Packages.
Search for FoxLearn.JsonLd.AspNetCore and click Install.
Next, we need to configure the services in your Program.cs
or Startup.cs
file. This involves adding the necessary services to the DI container and setting up structured data schemas.
Configure JSON-LD in Program.cs
(ASP.NET Core 6+):
builder.Services.ConfigureJsonLd<FoxLearn.JsonLd.Schema.Organization>(options => { options.Name = "FoxLearn"; options.Url = new Uri("https://foxlearn.com"); options.LegalName = "FoxLearn"; options.Logo = new Uri("https://foxlearn.com/img/logo.png"); options.Description = "Welcome to foxlearn.com! This site is a blog about everything that matters in the world of programming."; options.ContactPoint = new List<ContactPoint>() { new ContactPoint() { ContactType = "Customer Service", Name = "Tan Lee", Email = "[email protected]" } }; options.Founder = new List<IPerson>() { new Person() { Name = "Tan Lee" } }; options.FoundingDate = new FoxLearn.JsonLd.Date(2016, 01, 01); options.Email = "[email protected]"; }); builder.Services.AddScoped<IJsonLdBuilder, JsonLdBuilder>(); var app = builder.Build(); app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); app.MapStaticAssets(); app.UseJsonLd(); // Add this to enable JSON-LD injection in HTML app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}") .WithStaticAssets();
In the code above:
We configure JSON-LD for an Organization schema, setting properties like
Name
,Description
,Logo
,Founder
, etc.We add the
IJsonLdBuilder
service, which will be used to build the JSON-LD data dynamically.We call
app.UseJsonLd()
to automatically inject the generated JSON-LD data into your HTML<head>
.
Adding Structured Data in Controller or ViewModel
Below is an example of how to generate and add breadcrumb structured data on a category page.
private readonly IJsonLdBuilder _jsonLd; public HomeController(IJsonLdBuilder jsonLd) { _jsonLd = jsonLd; } public IActionResult Category(string categoryName) { // Create breadcrumb structured data BreadcrumbList breadcrumbList = new() { Name = "Breadcrumb", ItemListElement = new List<IListItem>() { new ListItem { Position = 1, Name = "Home", Url = new Uri($"{Request.Scheme}://{Request.Host}") }, new ListItem { Position = 2, Name = "Category", Url = new Uri($"{Request.Scheme}://{Request.Host}/{categoryName}") } } }; // Add the breadcrumb data to the JSON-LD builder _jsonLd.Add(breadcrumbList); return View(); }
In this example:
We create a
BreadcrumbList
schema, which is used to represent navigational breadcrumbs.The
IJsonLdBuilder
is used to add the breadcrumb data to the page.When the page is rendered, this structured data will be injected into the
<head>
section.
Output:
<script type="application/ld+json"> { "@context": "https://schema.org", "@type": "Organization", "email": "[email protected]", "founder": { "@type": "Person", "name": "Tan Lee" }, "foundingDate": "2016-01-01", "legalName": "FoxLearn", "contactPoint": { "@type": "ContactPoint", "email": "[email protected]", "contactType": "Customer Service", "name": "Tan Lee" }, "logo": "https://foxlearn.com/img/logo.png", "url": "https://foxlearn.com", "name": "FoxLearn", "description": "Welcome to foxlearn.com! This site is a blog about everything that matters in the world of programming." } </script>
This <script>
block will be automatically injected into the <head>
section of the HTML, and search engines can easily parse it to understand the structured data about your website.
You can use the Google Rich Results Test tool to verify that your JSON-LD data is correctly implemented and can be recognized by search engines.
Visit Google’s Rich Results Test.
Enter the URL of your page or paste the raw HTML.
Run the test to see if your structured data appears correctly.
By integrating FoxLearn.JsonLd.AspNetCore into your ASP.NET Core project, you can easily inject dynamic JSON-LD structured data into your pages, enhancing SEO and making your content more discoverable in search engines.
- How to Initialize TagHelpers in ASP.NET Core with Shared Data
- Boost Your ASP.NET Core Website Performance with .NET Profiler
- The name 'Session' does not exist in the current context
- Implementing Two-Factor Authentication with Google Authenticator in ASP.NET Core
- How to securely reverse-proxy ASP.NET Core
- How to Retrieve Client IP in ASP.NET Core Behind a Reverse Proxy
- Only one parameter per action may be bound from body in ASP.NET Core
- The request matched multiple endpoints in ASP.NET Core