![]() |
VOOZH | about |
dotnet add package OpenGraph-Net --version 4.0.1
NuGet\Install-Package OpenGraph-Net -Version 4.0.1
<PackageReference Include="OpenGraph-Net" Version="4.0.1" />
<PackageVersion Include="OpenGraph-Net" Version="4.0.1" />Directory.Packages.props
<PackageReference Include="OpenGraph-Net" />Project file
paket add OpenGraph-Net --version 4.0.1
#r "nuget: OpenGraph-Net, 4.0.1"
#:package OpenGraph-Net@4.0.1
#addin nuget:?package=OpenGraph-Net&version=4.0.1Install as a Cake Addin
#tool nuget:?package=OpenGraph-Net&version=4.0.1Install as a Cake Tool
👁 Build
👁 Nuget V
👁 Nuget dl
👁 License
👁 Contributor Covenant
👁 gitter
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.
If you find this library useful, buy me a coffee!
These are the basic operations of the OpenGraphNet parser.
Use async/await to parse a URL:
OpenGraph graph = await OpenGraph.ParseUrlAsync("https://open.spotify.com/user/er811nzvdw2cy2qgkrlei9sqe/playlist/2lzTTRqhYS6AkHPIvdX9u3?si=KcZxfwiIR7OBPCzj20utaQ");
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"
The four required Open Graph properties for all pages are available as direct properties on the OpenGraph object.
graph.Type is a shortcut for graph.Metadata["og:type"].Value()graph.Title is a shortcut for graph.Metadata["og:title"].Value()graph.Image is a shortcut for graph.Metadata["og:image"].Value()og:image:width then you
should instead use the graph.Metadata dictionary.*graph.Url is a shortcut for graph.Metadata["og:url"].Value()The original URL used to generate the OpenGraph data is available from the OriginalUrl property
graph.OriginalUrl.
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">
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.
title, type, image, urlIf there are any additional standard/supported namespaces that I am missing, please shoot me a comment or a pull request with the missing items.
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"
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> tagTo 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#">
head tagBelow 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>
So please don't be afraid to fork me.
develop branch.| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 is compatible. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 netstandard2.1 is compatible. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
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 |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 5.0.0-alpha.0.0.4 | 567 | 10/5/2025 |
| 5.0.0-alpha.0.0.3 | 174 | 10/5/2025 |
| 5.0.0-alpha.0.0.2 | 178 | 10/5/2025 |
| 4.0.2-alpha.0.13 | 209 | 1/17/2025 |
| 4.0.2-alpha.0.6 | 10,537 | 11/14/2022 |
| 4.0.1 | 483,648 | 8/12/2022 |
| 4.0.1-alpha.0.3 | 279 | 8/12/2022 |
| 4.0.1-alpha.0.2 | 279 | 8/12/2022 |
| 4.0.1-alpha.0.1 | 276 | 8/12/2022 |
| 4.0.0 | 28,780 | 8/12/2022 |
| 3.2.9-alpha.0.21 | 288 | 8/12/2022 |
| 3.2.9-alpha.0.20 | 267 | 8/12/2022 |
| 3.2.9-alpha.0.16 | 260 | 8/12/2022 |
| 3.2.9-alpha.0.15 | 268 | 8/12/2022 |
| 3.2.9-alpha.0.14 | 305 | 8/12/2022 |
| 3.2.9-alpha.0.10 | 271 | 8/12/2022 |
| 3.2.9-alpha.0.4 | 1,097 | 7/27/2021 |
| 3.2.9-alpha.0.3 | 389 | 7/27/2021 |
| 3.2.9-alpha.0.2 | 399 | 7/27/2021 |
| 3.2.8 | 228,219 | 7/27/2021 |
Added back NetStandard 2.1.