FoxLearn.AspNetCore.JsonLd
1.1.1
dotnet add package FoxLearn.AspNetCore.JsonLd --version 1.1.1
NuGet\Install-Package FoxLearn.AspNetCore.JsonLd -Version 1.1.1
<PackageReference Include="FoxLearn.AspNetCore.JsonLd" Version="1.1.1" />
<PackageVersion Include="FoxLearn.AspNetCore.JsonLd" Version="1.1.1" />
<PackageReference Include="FoxLearn.AspNetCore.JsonLd" />
paket add FoxLearn.AspNetCore.JsonLd --version 1.1.1
#r "nuget: FoxLearn.AspNetCore.JsonLd, 1.1.1"
#:package FoxLearn.AspNetCore.JsonLd@1.1.1
#addin nuget:?package=FoxLearn.AspNetCore.JsonLd&version=1.1.1
#tool nuget:?package=FoxLearn.AspNetCore.JsonLd&version=1.1.1
π· FoxLearn.AspNetCore.JsonLd
FoxLearn.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 <head>, 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.
β¨ Key Features
- β
Auto-injection of structured data
(application/ld+json)
- π Schema.org support for defining custom schemas
- βοΈ Seamless ASP.NET Core integration (.NET 6+)
- π« No manual
<script>
tag editing - π Boosts SEO for better search result presentation (e.g., breadcrumbs, rich cards)
π¦ Installation
Install 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.
βοΈ Getting Started
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
π Usage Guide
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);
}
}
π§© Razor Tag Integration (Alternative to Middleware)
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 <json-ld />, you can remove app.UseJsonLd()
from Program.cs
.
β Sample Output (SEO-Friendly JSON-LD)
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."
}
]
}
π SEO Benefits
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)
π§ Advanced Scenarios
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.
π License
This project is licensed under the MIT License. Use it freely in personal or commercial projects.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net6.0
- FoxLearn.JsonLd (>= 1.1.1)
-
net7.0
- FoxLearn.JsonLd (>= 1.1.1)
-
net8.0
- FoxLearn.JsonLd (>= 1.1.1)
-
net9.0
- FoxLearn.JsonLd (>= 1.1.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.