Adding structured data using partial views

Structured data is a type of metadata added to your web page that makes it easier for search engines to classify your web page, identify what your web page has to offer, and provide other important information about this page. One type of web page is a blog post and every blog post should have corresponding structured data. In this post, I explain how to facilitate adding structured data to each of your posts on a .net core website using a partial view and  the class ViewDataDictionary.  You can also use this as an example of sending data to any partial view using the class ViewDataDictionary.

Example Structured Data using JSON-LD

Here is an example of structured data for a blog post about how voice search affects your website.

<script type=”application/ld+json”>
{ “@context”: “http://schema.org”,
“@type”: “BlogPosting”,
“headline”:”How can my website profit from voice search?”,
“editor”: “Cheryl Hammond”,
“keywords”: “voice search, seo, long tailed search, Google featured snippet”,
“publisher”: “Today Data & Web Services, Inc.”,
“url”: “https://todaydata.com/Home/story/how-does-voice-search-affect-you”,
“datePublished”: “4/30/2019”,
“description”: “how voice search affects seo”,
“author”: {
“@type”: “Person”,
“name”: “Cheryl Hammond”
}
}

</script>

Creating the partial view

Some of this content will remain the same for the next post you make and it helps to have an easier way to add this structured data. I created a partial view called “sdata”, like this.

<script type=”application/ld+json”>
{ “@@context”: “http://schema.org”,
“@@type”: “BlogPosting”,
“headline”:”@this.ViewData[“headline”]”,
“editor”: “Cheryl Hammond”,
“keywords”: “@this.ViewData[“keywords”]”,
“publisher”: “Today Data & Web Services, Inc.”,
“url”: “@this.ViewData[“url”]”,
“datePublished”: “@this.ViewData[“datePublished”]”,
“description”: “@this.ViewData[“description”]”,
“author”: {
“@@type”: “Person”,
“name”: “Cheryl Hammond”
}
}

</script>

As you can see the author is always the same. So, is the publisher, the context and the type.  I added Razor variables in this partial view for the parts that will change.

Implementing the partial view with the class ViewDataDictionary

When ready to use this partial view, I add the following.

@await Html.PartialAsync(
“sdata”,
new ViewDataDictionary(this.Vi‌​ewData) {
{“headline”, “How can my website profit from voice search?”},
{“keywords”, “voice search, seo, long tailed search, Google featured snippet” },
{“datePublished”,”4/30/2019″ },
{“description”,”how voice search affects seo” },
{“url”, “https://todaydata.com/Home/story/how-does-voice-search-affect-you” }
}
)

I can add this anywhere. It does not need to appear in the head section.  If you post several blog posts on the same page, you can have multiple instances of structured data. Besides structured data, be sure to also include the usual other metadata of various types, such as description.

Leave a Reply

Your email address will not be published. Required fields are marked *