FoxLearn.AspNetCore.JsonLd 1.0.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package FoxLearn.AspNetCore.JsonLd --version 1.0.4
                    
NuGet\Install-Package FoxLearn.AspNetCore.JsonLd -Version 1.0.4
                    
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.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FoxLearn.AspNetCore.JsonLd" Version="1.0.4" />
                    
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.0.4
                    
#r "nuget: FoxLearn.AspNetCore.JsonLd, 1.0.4"
                    
#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.0.4
                    
#: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.0.4
                    
Install as a Cake Addin
#tool nuget:?package=FoxLearn.AspNetCore.JsonLd&version=1.0.4
                    
Install as a Cake Tool

FoxLearn.AspNetCore.JsonLd

It's a lightweight .NET library that automatically inserts <script> tags with the MIME type application/ld+json into the <head> section of your HTML. This helps search engines better understand your web pages by adding structured data (JSON-LD), which can improve SEO and enhance how your content appears in search results.


Features

  • 🤖 Automatically injects structured data (JSON-LD)
  • 🔍 Enhances SEO and search visibility
  • ⚙️ Easy integration with ASP.NET Core applications
  • 🧩 Supports custom schema definitions using Schema.org formats
  • ✨ No manual HTML edits required

Installation

Install via the .NET CLI:

dotnet add package FoxLearn.AspNetCore.JsonLd

Or via the NuGet UI in Visual Studio by searching for FoxLearn.AspNetCore.JsonLd

Usage

Register the services in Startup.cs or Program.cs:

// Optional: Add default for each page

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 = "example@gmail.com"}
    };
    options.Founder = new List<IPerson>()
    {
        new Person() { Name = "Tan Lee"}
    };
    options.FoundingDate = new FoxLearn.JsonLd.Date(2016, 01, 01);
    options.Email = "example@gmail.com";
});

// Optional: modify SerializerOptions, using graph schema
builder.Services.AddJsonLd(options => {
    options.UseGraph = true;
});

var app = builder.Build();

...

app.UseHttpsRedirection();
app.UseRouting();

app.UseAuthorization();

app.MapStaticAssets();

app.UseJsonLd(); // Required

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}")
    .WithStaticAssets();

Add structured data in your controller or view model:

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 WebSite CreateWebSite() => new WebSite
    {
        Id = $"{Hosting}#website",
        Url = BuildUri(),
        Name = "FoxLearn",
        Description = "Welcome to foxlearn.com! This site is a blog about everything that matters in the world of programming.",
        InLanguage = "en-US",
        PotentialAction = new SearchAction
        {
            Target = new Uri($"{Hosting}/post/search?q={{search_term_string}}"),
            QueryInput = "required name=search_term_string"
        }
    };

    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(CreateWebSite());

        _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(CreateWebSite());

        _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(CreateWebSite());

        _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(CreateWebSite());

        _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(CreateWebSite());

        _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(CreateWebSite());

        _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()
    {
        _jsonLdBuilder.Add(CreateWebSite());

        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)
    {
        _jsonLdBuilder.Add(CreateWebSite());

        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)
    {
        _jsonLdBuilder.Add(CreateWebSite());

        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(LatestPost obj)
    {
        _jsonLdBuilder.Add(CreateWebSite());

        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)
        ));
    }
}

// HomeController.cs

private readonly IJsonLdBuilder _jsonLd;

public HomeController(IJsonLdBuilder jsonLd)
{
    _jsonLd = jsonLd;
}

public async Task<IActionResult> Index(int? page)
{
    var model = await _postRepository.GetPagedPostsAsync(PostType.Article, page ?? 1);
    _jsonLdGenerator.Home();
    return View(model);
}

Example Output

{
  "@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."
    }
  ]
}

License

This project is licensed under the MIT License.

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