Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Schema.org objects turned into strongly typed C# POCO classes for use in .NET. All classes can be serialized into JSON/JSON-LD and XML, typically used to represent structured data in the head section of html page.

License

NotificationsYou must be signed in to change notification settings

RehanSaeed/Schema.NET

Repository files navigation

Schema.NET Banner

Schema.NET NuGet PackageSchema.NET Azure Artifacts PackageSchema.NET NuGet Package DownloadsTwitter URLTwitter Follow

Schema.org objects turned into strongly typed C# POCO classes for use in .NET. All classes can be serialized into JSON/JSON-LD and XML, typically used to represent structured data in thehead section ofhtml page.

Simple Example

varwebsite=newWebSite(){AlternateName="An Alternative Name",Name="Your Site Name",Url=newUri("https://example.com")};varjsonLd=website.ToString();

The code above outputs the following JSON-LD:

{"@context":"https://schema.org","@type":"WebSite","alternateName":"An Alternative Name","name":"Your Site Name","url":"https://example.com"}

If writing the result into a<script> element, be sure to use the.ToHtmlEscapedString() method instead to avoid exposing your website to a Cross-Site Scripting attack. See theexample below.

What is Schema.org?

schema.org defines a set of standard classes and their properties for objects and services in the real world. This machine readable format is a common standard used across the web for describing things.

Where is Schema.org Used?

Websites

Websites can define Structured Data in thehead section of theirhtml to enable search engines to show richer information in their search results. Here is an example of howGoogle can display extended metadata about your site in it's search results.

Google Logo Structured Data Example

Using structured data inhtml requires the use of ascript tag with a MIME type ofapplication/ld+json like so:

<scripttype="application/ld+json">{ "@context":"https://schema.org", "@type":"Organization", "url":"https://www.example.com", "name":"Unlimited Ball Bearings Corp.", "contactPoint":{   "@type":"ContactPoint",   "telephone":"+1-401-555-1212",   "contactType":"Customer service" }}</script>
Important Security Notice

When serializing the result for a website's<script> tag, you should use the alternate.ToHtmlEscapedString() to avoid exposing yourself to a Cross-Site Scripting (XSS) vulnerability if some of the properties in your schema have been set from untrusted sources.Usage in an ASP.NET MVC project might look like this:

<scripttype="application/ld+json">    @Html.Raw(Model.Schema.ToHtmlEscapedString())</script>

Windows UWP Sharing

Windows UWP apps let you share data using schema.org classes.Here is an example showing how to share metadata about a book.

Classes & Properties

schema.org defines classes and properties, where each property can have a single value or an array of multiple values. Additionally, properties can have multiple types e.g. anAddress property could have a type ofstring or a type ofPostalAddress which has it's own properties such asStreetAddress orPostalCode which breaks up an address into it's constituent parts.

To facilitate this Schema.NET uses some clever C# generics and implicit type conversions so that setting a single or multiple values is possible and that setting astring orPostalAddress is also possible:

// Single string addressvarorganization=newOrganization(){Address="123 Old Kent Road E10 6RL"};// Multiple string addressesvarorganization=newOrganization(){Address=newList<string>(){"123 Old Kent Road E10 6RL","456 Finsbury Park Road SW1 2JS"}};// Single PostalAddress addressvarorganization=newOrganization(){Address=newPostalAddress(){StreetAddress="123 Old Kent Road",PostalCode="E10 6RL"}};// Multiple PostalAddress addressesvarorganization=newOrganization(){Address=newList<PostalAddress>(){newPostalAddress(){StreetAddress="123 Old Kent Road",PostalCode="E10 6RL"},newPostalAddress(){StreetAddress="456 Finsbury Park Road",PostalCode="SW1 2JS"}}};// Mixed Author typesvarbook=newBook(){Author=newList<object>(){newOrganization(){Name="Penguin"},newPerson(){Name="J.D. Salinger"}}};// Deconstruct a property containing mixed typesif(book.Author.HasValue){var(organisations,people)=book.Author.Value;}

This magic is all carried out usingimplicit conversion operators in theOneOrMany<T>,Values<T1, T2>,Values<T1, T2, T3> andValues<T1, T2, T3, T4> types. These types are allstructs for best performance too.

More Examples

For more examples and actual running code samples, take a look at the unit tests in the project source code.

Schema.NET.Pending

There are many pending types onschema.org which are not yet fully formed and ready for production. If you need to use these, you can install theSchema.NET.Pending NuGet package instead ofSchema.NET. This package contains all released schema types as well as all pending types.

Schema.NET.Pending NuGet PackageSchema.NET.Pending Azure Artifacts PackageSchema.NET.Pending NuGet Package Downloads

Continuous Integration

NameOperating SystemStatusHistory
Azure PipelinesUbuntuAzure Pipelines Ubuntu Build Status
Azure PipelinesMacAzure Pipelines Mac Build Status
Azure PipelinesWindowsAzure Pipelines Windows Build Status
Azure PipelinesOverallAzure Pipelines Overall Build StatusAzure DevOps Build History
GitHub ActionsUbuntu, Mac & WindowsGitHub Actions StatusGitHub Actions Build History
AppVeyorUbuntu, Mac & WindowsAppVeyor Build StatusAppVeyor Build History

Contributions and Thanks

Please view thecontributing guide for more information.

  • kirkone - CI reads .NET Core version from new global.json file.
  • Turnerj - AddedSystem.Text.Json support, Had all types implementIEquatable<T>GetHashCode and added extra unit tests and bug fixes.
  • shervinw - Added better null value handling for structs.
  • kirk-marple - Refactoring JSON serialization to be more efficient.
  • nickevansuk - Adding better null value handling and use HTTPS instead of HTTP.
  • MEmanuelsson - Added support for the schema.org Date type without time.
  • halovanic - For adding interfaces to Schema.NET types for greater flexibility.
  • AndreSteenbergen - For enabling the tool to work on linux.
  • benmccallum - For adding XSS vlnerability protection.
  • psampaio - Added deserialization support and unit tests.
  • icunningham88 - Improved a test.

About

Schema.org objects turned into strongly typed C# POCO classes for use in .NET. All classes can be serialized into JSON/JSON-LD and XML, typically used to represent structured data in the head section of html page.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Languages


[8]ページ先頭

©2009-2025 Movatter.jp