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.AspNet.JsonLdFoxLearn.AspNet.JsonLd is a lightweight, easy-to-use .NET library that automatically injects JSON-LD structured data into your ASP.NET 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.
🔍 Ideal for blogs, e-commerce platforms, and any ASP.NET MVC/WebForms application that aims to improve SEO with structured metadata.
(application/ld+json)<script> tag editingInstall via the .NET CLI:
dotnet add package FoxLearn.AspNet.JsonLd
Or via NuGet Package Manager
Search for FoxLearn.AspNet.JsonLd in Visual Studio’s NuGet UI.
📘 ASP.NET MVC Setup
1. Update Global.asax.cs
Modify the Application_Start method:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.Register<IJsonLdBuilder, JsonLdBuilder>();
builder.Register<JsonLdActionFilter>().FilterFor<Controller>();
builder.RegisterFilterProvider();
DependencyResolver.SetResolver(new ContainerBuilderDependencyResolver(builder));
}
📝 This setup automatically injects JSON-LD into your HTML <head> using action filters.
Using HTML Helper Instead of Filters
If you prefer manual injection
1. Modify Your Layout File
@using FoxLearn.AspNet.JsonLd
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
@Html.JsonLdScript()
</head>2. Remove Filter Registration
Remove the following lines from Application_Start:
//builder.Register<JsonLdActionFilter>().FilterFor<Controller>();
//builder.RegisterFilterProvider();3. Add JSON-LD to an Action
Example: Index action in HomeController:
public class HomeController : Controller
{
private readonly IJsonLdBuilder _jsonLdBuilder;
public HomeController(IJsonLdBuilder jsonLdBuilder)
{
_jsonLdBuilder = jsonLdBuilder;
}
public ActionResult Index()
{
_jsonLdBuilder.Add(new Organization
{
Name = "My Company",
Url = new Uri("https://example.com")
});
return View();
}
}📘 ASP.NET WebForms Setup
1. Install Required Packages
Autofac
Autofac.Web
Autofac.Mvc5
2. Update web.config
<system.webServer>
<modules>
<add name="ContainerDisposal" type="Autofac.Integration.Web.ContainerDisposalModule, Autofac.Integration.Web" preCondition="managedHandler"/>
<add name="PropertyInjection" type="Autofac.Integration.Web.Forms.PropertyInjectionModule, Autofac.Integration.Web" preCondition="managedHandler"/>
</modules>
</system.webServer>3. Modify Global.asax.cs
public class Global : HttpApplication, IContainerProviderAccessor
{
static IContainerProvider _containerProvider;
public IContainerProvider ContainerProvider => _containerProvider;
void Application_Start(object sender, EventArgs e)
{
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
var builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(typeof(Global).Assembly)
.Where(t => t.IsSubclassOf(typeof(System.Web.UI.Page)))
.PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies);
builder.RegisterType<JsonLdBuilder>()
.As<IJsonLdBuilder>()
.InstancePerRequest();
var container = builder.Build();
_containerProvider = new ContainerProvider(container);
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
void Application_BeginRequest(object sender, EventArgs e)
{
var contextBase = new HttpContextWrapper(Context);
JsonLdHandler.ApplyJsonLdFilter(contextBase);
}
}4. Inject JSON-LD on WebForm Page
Example: Contact.aspx.cs
public partial class Contact : Page
{
public IJsonLdBuilder JsonLdBuilder { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (JsonLdBuilder != null)
{
var page = new ContactPage
{
Name = "Contact Us"
};
JsonLdBuilder.Add(page);
}
}
}This project is licensed under the MIT License. Use it freely in personal or commercial projects.