Created
December 28, 2019 16:35
-
-
Save tristolliday/220f5f33762bf2492a30a5f1557d4a72 to your computer and use it in GitHub Desktop.
Newtonsoft Json.NET for Structured Data - Google Rich Snippets and Enhanced SEO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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