OpenGraph-Net
5.0.0-alpha.0.0.2
See the version list below for details.
dotnet add package OpenGraph-Net --version 5.0.0-alpha.0.0.2
NuGet\Install-Package OpenGraph-Net -Version 5.0.0-alpha.0.0.2
<PackageReference Include="OpenGraph-Net" Version="5.0.0-alpha.0.0.2" />
<PackageVersion Include="OpenGraph-Net" Version="5.0.0-alpha.0.0.2" />
<PackageReference Include="OpenGraph-Net" />
paket add OpenGraph-Net --version 5.0.0-alpha.0.0.2
#r "nuget: OpenGraph-Net, 5.0.0-alpha.0.0.2"
#:package OpenGraph-Net@5.0.0-alpha.0.0.2
#addin nuget:?package=OpenGraph-Net&version=5.0.0-alpha.0.0.2&prerelease
#tool nuget:?package=OpenGraph-Net&version=5.0.0-alpha.0.0.2&prerelease
OpenGraphNet
A simple .net assembly to use to parse Open Graph information from either a URL or an HTML snippet. You can read more about the Open Graph protocol @ http://ogp.me.
Support the library
If you find this library useful, buy me a coffee!
Usage
These are the basic operations of the OpenGraphNet parser.
Parsing from a URL
Use async/await to parse a URL:
OpenGraph graph = await OpenGraph.ParseUrlAsync("https://open.spotify.com/user/er811nzvdw2cy2qgkrlei9sqe/playlist/2lzTTRqhYS6AkHPIvdX9u3?si=KcZxfwiIR7OBPCzj20utaQ");
Accessing Values
Accessing Metadata
Each metadata element is stored as an array. Additionally, each element's properties are also stored as an array.
<meta property="og:image" content="http://example.com/img1.png">
<meta property="og:image:width" content="30">
<meta property="og:image" content="http://example.com/img2.png">
<meta property="og:image:width" content="60">
<meta property="og:locale" content="en">
<meta property="og:locale:alternate" content="en_US">
<meta property="og:locale:alternate" content="en_GB">
You would access the values from the sample HTML above as:
graph.Metadata["og:image"].First().Value; // "http://example.com/img1.png"
graph.Metadata["og:image"].First().Properties["width"].Value(); // "30"
graph.Metadata["og:image"][1].Value; // "http://example.com/img2.png"
graph.Metadata["og:image"][1].Properties["width"].Value(); // "30"
graph.Metadata["og:locale"].Value(); // "en"
graph.Metadata["og:locale"].First().Properties["alternate"][0].Value; // "en_US"
graph.Metadata["og:locale"].First().Properties["alternate"][1].Value; // "en_GB"
Basic Metadata
The four required Open Graph properties for all pages are available as direct properties on the OpenGraph object.
graph.Typeis a shortcut forgraph.Metadata["og:type"].Value()graph.Titleis a shortcut forgraph.Metadata["og:title"].Value()graph.Imageis a shortcut forgraph.Metadata["og:image"].Value()- Note: since there can be multiple images, this helper returns the URI of the
first image. If you want to access images or child properties like
og:image:widththen you should instead use thegraph.Metadatadictionary.* graph.Urlis a shortcut forgraph.Metadata["og:url"].Value()
Misc
The original URL used to generate the OpenGraph data is available from the OriginalUrl property
graph.OriginalUrl.
Creating OpenGraph Data
To create OpenGraph data in memory use the following code:
var graph = OpenGraph.MakeGraph(
title: "My Title",
type: "website",
image: "http://example.com/img/img1.png",
url: "http://example.com/home",
description: "My Description",
siteName: "Example.com");
graph.AddMetadata("og", "image", "http://example.com/img/img2.png");
graph.Metadata["og:image"][0].AddProperty("width", "30");
graph.Metadata["og:image"][1].AddProperty("width", "60");
System.Console.Write(graph.ToString());
The previous System.Console.Write(graph.ToString()); will produce the following HTML (formatting added for legibility):
<meta property="og:title" content="My Title">
<meta property="og:type" content="website">
<meta property="og:image" content="http://example.com/img/img1.png">
<meta property="og:image:width" content="30">
<meta property="og:image" content="http://example.com/img/img2.png">
<meta property="og:image:width" content="60">
<meta property="og:url" content="http://example.com/home">
<meta property="og:description" content="My Description">
<meta property="og:site_name" content="Example.com">
Parsing Namespaces
The component now knows about the 13 namespaces listed below. When parsing a url or a HTML
document, OpenGraph.Net will now read and use those namespaces from either the <html> or
<head> tags. The parser is now smart enough to include the namespaces when none are
included in those tags by extracting it from the meta[property] value directly.
- og: http://ogp.me/ns#
- Expected fields when validating:
title,type,image,url
- Expected fields when validating:
- article: http://ogp.me/ns/article#
- book: http://ogp.me/ns/book#
- books: http://ogp.me/ns/books#
- business http://ogp.me/ns/business#
- fitness: http://ogp.me/ns/fitness#
- game: http://ogp.me/ns/game#
- music: http://ogp.me/ns/music#
- place: http://ogp.me/ns/place#
- product: http://ogp.me/ns/product#
- profile: http://ogp.me/ns/profile#
- restaurant: http://ogp.me/ns/restaurant#
- video: http://ogp.me/ns/video#"
If there are any additional standard/supported namespaces that I am missing, please shoot me a comment or a pull request with the missing items.
Adding Custom Namespaces
You can now add custom namespaces to the parser. Simply make the following call:
NamespaceRegistry.Instance.AddNamespace(
prefix: "gah",
schemaUri: "http://wwww.geoffhorsey.com/ogp/brain#",
requiredElements: new[] { "brain" });
Doing the above will allow the parser to understand the following HTML snippet:
<meta property="gah:brain" content="http://www.geoffhorsey.com/my-brain">
<meta property="gah:brain:size" content="tiny">
and the graph:
graph.Metadata["gah:brain"].Value() // "http://www.geoffhorsey.com/my-brain"
graph.Metadata["gah:brain"].First().Properties["size"].Value() // "tiny"
Writing out OpenGraph Namespaces
In the wild web sites seem to add their OpenGraph namespaces in one of 2 ways. They either
write the namespaces in the html as xmlns attributes or within the head tag in the prefix attribute.
<html xmlns:og="http://ogp.me/ns#" xmlns:product="http://ogp.me/ns/product#"><head prefix="og: http://ogp.me/ns# product: http://ogp.me/ns/product#">
xmlns: version in the html tag
To create the html version in an cshtml page after creating a new graph, use the following code:
<html @graph.HtmlXmlnsValues>
Would produce the following:
<html xmlns:og="http://ogp.me/ns#" xmlns:product="http://ogp.me/ns/product#">
prefix version in the <head> tag
To create the head version in a cshtml page, after create a new graph, use the following code:
<head prefix="@graph.HeadPrefixAttributeValue">
Would produce the following:
<head prefix="og: http://ogp.me/ns# product: http://ogp.me/ns/product#">
Writing out OpenGraph Metadata to the head tag
Below is a complete example to write out a OpenGraph metadata to a page:
@{
var graph = OpenGraph.MakeGraph(
title: "My Title",
type: "website",
image: "http://example.com/img/img1.png",
url: "http://example.com/home",
description: "My Description",
siteName: "Example.com");
}
<html>
<head prefix="@graph.HeadPrefixAttributeValue">
@graph.ToString()
</head>
<body>
</body>
</html>
will produce the following HTML:
<html>
<head prefix="og: http://ogp.me/ns#">
<meta property="og:title" content="My Title">
<meta property="og:type" content="website">
<meta property="og:image" content="http://example.com/img/img1.png">
<meta property="og:url" content="http://example.com/home">
<meta property="og:description" content="My Description">
<meta property="og:site_name" content="Example.com">
</head>
<body>
</body>
</html>
It's FOSS
So please don't be afraid to fork me.
Contribution Guide
- Fork the OpenGraph-Net repository
- Create a feature branch for the item you are going to add.
- Add your awesome code and your unit tests to cover the new feature
- Run all of the tests to ensure everything is still passing.
- Create a pull request to our
developbranch.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- HtmlAgilityPack (>= 1.12.4)
- System.Text.Encoding.CodePages (>= 9.0.9)
NuGet packages (5)
Showing the top 5 NuGet packages that depend on OpenGraph-Net:
| Package | Downloads |
|---|---|
|
RestCms.Api.NETCore
This is the main REST CMS API package. This package contains initialization code, controllers, filters and core middleware required to run the REST CMS client API. |
|
|
Zen.Web.App
WebApp-related abstration layer for Zen |
|
|
ImageWizard.OpenGraph
Image processing middleware based on ASP.NET and ImageSharp / SkiaSharp / SvgNet. |
|
|
Wechaty
dotnet wechaty |
|
|
Rochas.ImageGenerator
A simple base64 image and qrcode generator |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 5.0.0-alpha.0.0.4 | 256 | 10/5/2025 |
| 5.0.0-alpha.0.0.3 | 124 | 10/5/2025 |
| 5.0.0-alpha.0.0.2 | 123 | 10/5/2025 |
| 4.0.2-alpha.0.13 | 154 | 1/17/2025 |
| 4.0.2-alpha.0.6 | 5,762 | 11/14/2022 |
| 4.0.1 | 383,716 | 8/12/2022 |
| 4.0.1-alpha.0.3 | 212 | 8/12/2022 |
| 4.0.1-alpha.0.2 | 215 | 8/12/2022 |
| 4.0.1-alpha.0.1 | 212 | 8/12/2022 |
| 4.0.0 | 16,226 | 8/12/2022 |
| 3.2.9-alpha.0.21 | 229 | 8/12/2022 |
| 3.2.9-alpha.0.20 | 208 | 8/12/2022 |
| 3.2.9-alpha.0.16 | 203 | 8/12/2022 |
| 3.2.9-alpha.0.15 | 210 | 8/12/2022 |
| 3.2.9-alpha.0.14 | 241 | 8/12/2022 |
| 3.2.9-alpha.0.10 | 219 | 8/12/2022 |
| 3.2.9-alpha.0.4 | 1,062 | 7/27/2021 |
| 3.2.9-alpha.0.3 | 324 | 7/27/2021 |
| 3.2.9-alpha.0.2 | 344 | 7/27/2021 |
| 3.2.8 | 208,691 | 7/27/2021 |
| 3.2.7 | 4,567 | 6/8/2021 |
| 3.2.7-alpha.0.40 | 285 | 6/8/2021 |
| 3.2.7-alpha.0.39 | 266 | 6/8/2021 |
| 3.2.7-alpha.0.38 | 306 | 6/8/2021 |
| 3.2.7-alpha.0.37 | 295 | 6/8/2021 |
| 3.2.7-alpha.0.35 | 303 | 6/8/2021 |
| 3.2.7-alpha.0.34 | 276 | 6/8/2021 |
| 3.2.6 | 32,841 | 1/28/2021 |
| 3.2.5 | 612 | 1/28/2021 |
| 3.2.4 | 72,373 | 3/24/2020 |
| 3.2.3 | 2,507 | 2/26/2020 |
| 3.2.2 | 780 | 2/26/2020 |
| 3.2.1 | 16,117 | 2/7/2020 |
| 3.2.0 | 15,538 | 6/15/2019 |
| 3.1.2 | 41,923 | 3/16/2019 |
| 3.1.1 | 1,047 | 2/28/2019 |
| 3.1.0 | 2,265 | 1/18/2019 |
| 3.0.0 | 14,029 | 12/5/2018 |
| 2.2.0 | 43,128 | 5/10/2018 |
| 2.0.0 | 1,766 | 5/2/2018 |
| 1.3.4 | 69,265 | 8/17/2016 |
| 1.3.3 | 1,728 | 8/16/2016 |
| 1.3.2 | 1,904 | 8/15/2016 |
| 1.3.1 | 1,862 | 8/15/2016 |
| 1.3.0.1 | 7,236 | 6/30/2016 |
| 1.2.0.1 | 9,119 | 4/2/2013 |
Added back NetStandard 2.1.