Skip to main content
Home  › ... Razor

Razor Page Service Tutorials

Tutorial Home
Requirements
Resources

IPageService to add JSON-LD Headers

This page adds 2 JSON-LD tags to the html-head. View the page source to see it.

Info about the Base Class

This tutorial inherits from the Custom.Hybrid.Razor14 base class.

This allows us to use Kit.Page to access an IPageService without having to use GetService<IPageService>

@{
  // example adding a json-ld as a string
  Kit.Page.AddJsonLd("{ \"@context\": \"https://schema.org/\" }");
}
@{
  // creating a JSON-LD using an object - replicating googles example https://developers.google.com/search/docs/guides/intro-structured-data
  var jsonLd = new Dictionary<string, object> {
    { "@context", "https://schema.org"},
    { "@type", "Organization"},
    { "url", "http://www.example.com"},
    { "name", "Unlimited Ball Bearings Corp."},
    { "contactPoint", new Dictionary<string, object> {
      {"@type", "ContactPoint"},
      {"telephone", "+1-401-555-1212"},
      {"contactType", "Customer service"}
    }}
  };

  Kit.Page.AddJsonLd(jsonLd);
}

Source Code of this file

Below you'll see the source code of the file. Note that we're just showing the main part, and hiding some parts of the file which are not relevant for understanding the essentials. Click to expand the code

@inherits Custom.Hybrid.Razor14
@using ToSic.Razor.Blade;
<!-- unimportant stuff, hidden -->


<div @Sys.PageParts.InfoWrapper()>
  @Html.Partial("../shared/_DefaultInfoSection.cshtml")
  <div @Sys.PageParts.InfoIntro()>
      <h2><em>IPageService</em> to add JSON-LD Headers </h2>
      <div>
        This page adds 2 JSON-LD tags to the html-head. View the page source to see it.
      </div>
    </div>
  </div>


@Html.Partial("../shared/_KitBaseClassInfoBox.cshtml", new { ServiceName = "Page", Service = "IPageService" })

  @{
    // example adding a json-ld as a string
    Kit.Page.AddJsonLd("{ \"@context\": \"https://schema.org/\" }");
  }
  @{
    // creating a JSON-LD using an object - replicating googles example https://developers.google.com/search/docs/guides/intro-structured-data
    var jsonLd = new Dictionary<string, object> {
      { "@context", "https://schema.org"},
      { "@type", "Organization"},
      { "url", "http://www.example.com"},
      { "name", "Unlimited Ball Bearings Corp."},
      { "contactPoint", new Dictionary<string, object> {
        {"@type", "ContactPoint"},
        {"telephone", "+1-401-555-1212"},
        {"contactType", "Customer service"}
      }}
    };

    Kit.Page.AddJsonLd(jsonLd);
  }