1
+ @using System .Text .Json
2
+ @using System .Text .Json .Serialization
3
+
4
+ <HeadContent >
5
+ <script suppress-error =" BL9992" type =" application/ld+json" >@StructuredDataJson </script >
6
+ </HeadContent >
7
+
8
+ @code {
9
+ [Parameter ]
10
+ public string Headline { get ; set ; }
11
+
12
+ [Parameter ]
13
+ public string PreviewImage { get ; set ; }
14
+
15
+ [Parameter ]
16
+ public string PreviewFallbackImage { get ; set ; }
17
+
18
+ [Parameter ]
19
+ public DateTime PublishedDate { get ; set ; }
20
+
21
+ [Parameter ]
22
+ public string Author { get ; set ; }
23
+
24
+ private string StructuredDataJson { get ; set ; }
25
+
26
+ private readonly JsonSerializerOptions jsonOptions = new () { WriteIndented = true };
27
+
28
+ protected override async Task OnParametersSetAsync ()
29
+ {
30
+ var article = new NewsArticle
31
+ {
32
+ Context = " https://schema.org" ,
33
+ Type = " NewsArticle" ,
34
+ Headline = Headline ,
35
+ Author = Author ,
36
+ Image = new List <string > { PreviewImage },
37
+ DatePublished = PublishedDate .ToString (" o" ),
38
+ DateModified = PublishedDate .ToString (" o" )
39
+ };
40
+
41
+ if (! string .IsNullOrWhiteSpace (PreviewFallbackImage ))
42
+ {
43
+ article .Image .Add (PreviewFallbackImage );
44
+ }
45
+
46
+ StructuredDataJson = JsonSerializer .Serialize (article , jsonOptions );
47
+
48
+ await base .OnParametersSetAsync ();
49
+ }
50
+
51
+ private class NewsArticle
52
+ {
53
+ [JsonPropertyName (" @context" )]
54
+ public string Context { get ; set ; }
55
+
56
+ [JsonPropertyName (" @type" )]
57
+ public string Type { get ; set ; }
58
+
59
+ [JsonPropertyName (" headline" )]
60
+ public string Headline { get ; set ; }
61
+
62
+ [JsonPropertyName (" author" )]
63
+ public string Author { get ; set ; }
64
+
65
+ [JsonPropertyName (" image" )]
66
+ public List <string > Image { get ; set ; }
67
+
68
+ [JsonPropertyName (" datePublished" )]
69
+ public string DatePublished { get ; set ; }
70
+
71
+ [JsonPropertyName (" dateModified" )]
72
+ public string DateModified { get ; set ; }
73
+ }
74
+ }
0 commit comments