-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathStructuredData.razor
More file actions
72 lines (53 loc) · 1.81 KB
/
Copy pathStructuredData.razor
File metadata and controls
72 lines (53 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
@using System.Text.Json
@using System.Text.Json.Serialization
<script suppress-error="BL9992" type="application/ld+json">@StructuredDataJson</script>
@code {
[Parameter, EditorRequired]
public required string Headline { get; set; }
[Parameter, EditorRequired]
public required string PreviewImage { get; set; }
[Parameter]
public string? PreviewFallbackImage { get; set; }
[Parameter, EditorRequired]
public DateTime PublishedDate { get; set; }
[Parameter, EditorRequired]
public required string Author { get; set; }
private MarkupString StructuredDataJson { get; set; }
private readonly JsonSerializerOptions jsonOptions = new() { WriteIndented = true };
protected override async Task OnParametersSetAsync()
{
var article = new NewsArticle
{
Context = "https://schema.org",
Type = "NewsArticle",
Headline = Headline,
Author = Author,
Image = [PreviewImage],
DatePublished = PublishedDate.ToString("o"),
DateModified = PublishedDate.ToString("o")
};
if (!string.IsNullOrWhiteSpace(PreviewFallbackImage))
{
article.Image.Add(PreviewFallbackImage);
}
StructuredDataJson = (MarkupString)JsonSerializer.Serialize(article, jsonOptions);
await base.OnParametersSetAsync();
}
private sealed class NewsArticle
{
[JsonPropertyName("@context")]
public required string Context { get; set; }
[JsonPropertyName("@type")]
public required string Type { get; set; }
[JsonPropertyName("headline")]
public required string Headline { get; set; }
[JsonPropertyName("author")]
public required string Author { get; set; }
[JsonPropertyName("image")]
public List<string> Image { get; set; } = [];
[JsonPropertyName("datePublished")]
public required string DatePublished { get; set; }
[JsonPropertyName("dateModified")]
public required string DateModified { get; set; }
}
}