Skip to content

Instantly share code, notes, and snippets.

@brendan-rice
Forked from tristolliday/QandA.cshtml
Created July 2, 2021 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brendan-rice/fd563b3106b193ab01366838479888d7 to your computer and use it in GitHub Desktop.
Save brendan-rice/fd563b3106b193ab01366838479888d7 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