Skip to content

Instantly share code, notes, and snippets.

@tristolliday
Created December 28, 2019 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tristolliday/220f5f33762bf2492a30a5f1557d4a72 to your computer and use it in GitHub Desktop.
Save tristolliday/220f5f33762bf2492a30a5f1557d4a72 to your computer and use it in GitHub Desktop.
Newtonsoft Json.NET for Structured Data - Google Rich Snippets and Enhanced SEO
@using Newtonsoft.Json.Linq
@{
var questions = Model.Value<IEnumerable<IPublishedElement>>("questions");
}
<script type="application/ld+json">
@{
var structuredData =
new JObject(
new JProperty("@context", "https://schema.org"),
new JProperty("@type", "FAQPage"),
new JProperty("mainEntity",
new JArray(
from item in questions
select new JObject(
new JProperty("@type", "Question"),
new JProperty("name", (string)(item.GetProperty("question").Value().ToString())),
new JProperty("acceptedAnswer",
new JObject(
new JProperty("@type", "Answer"),
new JProperty("text", (string)(item.GetProperty("answer").Value().ToString()))
)
)
)
)
)
);
}
@Html.Raw(structuredData.ToString())
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment