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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FoxLearn.AspNetCore.JsonLd" Version="1.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FoxLearn.AspNetCore.JsonLd" Version="1.1.1" />
                    
Directory.Packages.props
<PackageReference Include="FoxLearn.AspNetCore.JsonLd" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FoxLearn.AspNetCore.JsonLd --version 1.1.1
                    
#r "nuget: FoxLearn.AspNetCore.JsonLd, 1.1.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package FoxLearn.AspNetCore.JsonLd@1.1.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FoxLearn.AspNetCore.JsonLd&version=1.1.1
                    
Install as a Cake Addin
#tool nuget:?package=FoxLearn.AspNetCore.JsonLd&version=1.1.1
                    
Install as a Cake Tool

πŸ”· 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.1 146 6/18/2025
1.1.0 161 6/14/2025
1.0.4 251 6/13/2025
1.0.3 285 6/12/2025
1.0.2 282 6/12/2025
1.0.1 237 6/9/2025
1.0.0 98 6/6/2025