By Isidore Bennett Wednesday, October 31, 2018 Updated: Monday, November 5, 2018 10:28:14 AM SAST



Scenario

Let's say you have many Views that share the same layout but the information in them are different, kinda like a blog or news feed. It'd be nice to have Structured Data dynamically built based on what you pull from a data source, assuming you have a data source. I'mma try to walk you through the process.
This is my story.

Overview

In this snippet I will walk you through the process of dynamically building Stuctured Data in JSON-LD format in MVC ASP.NET for an Article. We will be using some C# and some Razor syntax to build the Stuctured Data in our View. For this example I will assume you have a data source and I'll be using a simple list that I'll pass to the view.

Straight Into It

I've chosen the Snippet format because a full length, start to end article would've been too long. So I'll be making a few assumptions here and there like you having access to a data source, however basic as it may be (it could be a plain ol' like the one I'll be using). I'll still try to give as much context and information as possible.

Model

First we'll update or create a Model that will be responsible for your Article. This Model will need to to have recommened properties as described on Google's guide on Structured Data for Articles. So:

  • dateModified
  • datePublished
  • headline
  • image

There's more than just these four but fortunately enough these are the four that are most likely to be subject to change across views.

There's more properties that could lead to Rich Cards being enabled by Google which is nice.
Google says: "The more recommended properties that you provide, the higher quality the result is to users. For example: users prefer job postings with explicitly stated salaries than those without; users prefer recipes with actual user reviews and genuine star ratings (note that reviews or ratings not by actual users are considered spammy). Rich result ranking takes extra information into consideration."

We're looking at "NON-AMP" compatible Structured Data because it requires sooooo much less set up, or at least from what I've seen but you're more than free to use Structured Data compatible with AMP sites.

Add these lines to your Model:

  1. public DateTime dateModified { get; set; }
  2. public DateTime datePublished { get; set; }
  3. public string headline { get; set; }
  4. public string image { get; set; }

Controller

In the Controller that will be handling the passing of your Model's data to your View, be sure to populate the four properties we've declared with the relevant data. dateModified and datePublished need to adhere to ISO 8601 format. You can do the date conversion in the Controller or you can do the conversion in the View. In the Controller it will look something like this: [Your_Modal_Here].dateModified.ToString("s", System.Globalization.CultureInfo.InvariantCulture) and the same goes for datePublished.

View

In your View's <head> tag we'll be adding the <script> tag that will house our structured data.
Below is an example of what I had as my structured data when I had written this article:

  1. <script type="application/ld+json">
  2.  {
  3.   "@@context": "http://schema.org",
  4.   "@@type": "Article",
  5.   "mainEntityOfPage": {
  6.    "@@type": "WebPage",
  7.    "@@id": "[The URL of where the article can be found]"
  8.  },
  9.  "headline": "@Model.headline",
  10.  "image": {
  11.   "@@type": "ImageObject",
  12.   "url": "@Model.image"
  13.  },
  14.  "datePublished": "@Model.datePublished.ToString("s", System.Globalization.CultureInfo.InvariantCulture)",
  15.  "dateModified": "@Model.dateModified.ToString("s", System.Globalization.CultureInfo.InvariantCulture)",
  16.  "author": {
  17.   "@@type": "Person",
  18.   "Name": "[Your or the author's name here]"
  19.  },
  20.   "publisher": {
  21.    "@@type":"Organization",
  22.    "name":"[Name of your publisher here]",
  23.   "logo":{
  24.    "@@type": "ImageObject",
  25.    "url": "[Your publisher's logo URL here]"
  26.   }
  27.  },
  28.  "thumbnailUrl": "[Your article's thumbnail URL here]"
  29.  }
  30. </script>

Replace the values in the square backets with the relevent information. These values can also be handled with the a Model, just add as many properties as you see fit. A more fleshed out Structured Data set is surely more preferred but be sure to stick to guidelines. Google has an example that you should look at.
Also notice that in lines 14 and 15 I've done the coversion to ISO 8601, where you decide to do the formatting is up to you.

You're able to test the validity of any sites Structured Data here and you can even take the URL of this page and test it. And you're even able to test whether your Structured Data supports Rich Results here.

Here's what the Structured Data for this article looks like when tested in the Structured Data Testing Tool:

Don't mind the dates being swapped around too much loool, it's a bug that needs fixing:P


Thanks for reading.


Privacy Policy