OpenGraph-Net
3.2.4
See the version list below for details.
dotnet add package OpenGraph-Net --version 3.2.4
NuGet\Install-Package OpenGraph-Net -Version 3.2.4
<PackageReference Include="OpenGraph-Net" Version="3.2.4" />
paket add OpenGraph-Net --version 3.2.4
#r "nuget: OpenGraph-Net, 3.2.4"
// Install OpenGraph-Net as a Cake Addin #addin nuget:?package=OpenGraph-Net&version=3.2.4 // Install OpenGraph-Net as a Cake Tool #tool nuget:?package=OpenGraph-Net&version=3.2.4
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.
Usage
These are the basic operations of the OpenGraphNet parser.
Parsing from a URL
Synchronosly parse a url:
OpenGraph graph = OpenGraph.ParseUrl("https://open.spotify.com/user/er811nzvdw2cy2qgkrlei9sqe/playlist/2lzTTRqhYS6AkHPIvdX9u3?si=KcZxfwiIR7OBPCzj20utaQ");
Use async/await
to parse a url:
OpenGraph graph = await OpenGraph.ParseUrlAsync("https://open.spotify.com/user/er811nzvdw2cy2qgkrlei9sqe/playlist/2lzTTRqhYS6AkHPIvdX9u3?si=KcZxfwiIR7OBPCzj20utaQ");
Acessing Values
Accessing Metadata
Each metadata element is 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.Type
is a shortcut forgraph.Metadata["og:type"].Value()
graph.Title
is a shortcut forgraph.Metadata["og:title"].Value()
graph.Image
is 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 likeog:image:width
then you should instead use thegraph.Metadata
dictionary.graph.Url
is a shortcut forgraph.Metadata["og:url"].Value()
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">
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 feture
- Run all of the tests to ensure everything is still passing.
- Create a pull request to our
develop
branch.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 is compatible. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.7.2
- HtmlAgilityPack (>= 1.11.23)
- System.Text.Encoding.CodePages (>= 4.7.0)
-
.NETStandard 2.0
- HtmlAgilityPack (>= 1.11.23)
- System.Text.Encoding.CodePages (>= 4.7.0)
-
.NETStandard 2.1
- HtmlAgilityPack (>= 1.11.23)
- System.Text.Encoding.CodePages (>= 4.7.0)
NuGet packages (4)
Showing the top 4 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 |
|
Wechaty
dotnet wechaty |
|
ImageWizard.OpenGraph
Image processing middleware based on ASP.NET and ImageSharp / SkiaSharp / SvgNet. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
4.0.2-alpha.0.6 | 3,997 | 11/14/2022 |
4.0.1 | 211,027 | 8/12/2022 |
4.0.1-alpha.0.3 | 120 | 8/12/2022 |
4.0.1-alpha.0.2 | 127 | 8/12/2022 |
4.0.1-alpha.0.1 | 122 | 8/12/2022 |
4.0.0 | 5,074 | 8/12/2022 |
3.2.9-alpha.0.21 | 119 | 8/12/2022 |
3.2.9-alpha.0.20 | 122 | 8/12/2022 |
3.2.9-alpha.0.16 | 118 | 8/12/2022 |
3.2.9-alpha.0.15 | 123 | 8/12/2022 |
3.2.9-alpha.0.14 | 124 | 8/12/2022 |
3.2.9-alpha.0.10 | 116 | 8/12/2022 |
3.2.9-alpha.0.4 | 898 | 7/27/2021 |
3.2.9-alpha.0.3 | 221 | 7/27/2021 |
3.2.9-alpha.0.2 | 236 | 7/27/2021 |
3.2.8 | 166,625 | 7/27/2021 |
3.2.7 | 4,304 | 6/8/2021 |
3.2.7-alpha.0.40 | 166 | 6/8/2021 |
3.2.7-alpha.0.39 | 166 | 6/8/2021 |
3.2.7-alpha.0.38 | 175 | 6/8/2021 |
3.2.7-alpha.0.37 | 167 | 6/8/2021 |
3.2.7-alpha.0.35 | 176 | 6/8/2021 |
3.2.7-alpha.0.34 | 174 | 6/8/2021 |
3.2.6 | 32,452 | 1/28/2021 |
3.2.5 | 441 | 1/28/2021 |
3.2.4 | 69,598 | 3/24/2020 |
3.2.3 | 2,247 | 2/26/2020 |
3.2.2 | 583 | 2/26/2020 |
3.2.1 | 15,961 | 2/7/2020 |
3.2.0 | 14,737 | 6/15/2019 |
3.1.2 | 41,716 | 3/16/2019 |
3.1.1 | 885 | 2/28/2019 |
3.1.0 | 2,079 | 1/18/2019 |
3.0.0 | 13,802 | 12/5/2018 |
2.2.0 | 37,459 | 5/10/2018 |
2.0.0 | 1,325 | 5/2/2018 |
1.3.4 | 64,334 | 8/17/2016 |
1.3.3 | 1,215 | 8/16/2016 |
1.3.2 | 1,288 | 8/15/2016 |
1.3.1 | 1,273 | 8/15/2016 |
1.3.0.1 | 6,791 | 6/30/2016 |
1.2.0.1 | 8,370 | 4/2/2013 |
Added support for target framework 472 to resolve issue 14.