Repository navigation
Expand file tree
/
Copy pathHomeController.cs
More file actions
191 lines (174 loc) · 8.19 KB
/
Copy pathHomeController.cs
File metadata and controls
191 lines (174 loc) · 8.19 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using DotnetSitemapGenerator;
using EssentialCSharp.Web.Extensions;
using EssentialCSharp.Web.Helpers;
using EssentialCSharp.Web.Middleware;
using EssentialCSharp.Web.Models;
using EssentialCSharp.Web.Services;
using HtmlAgilityPack;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OutputCaching;
using Microsoft.AspNetCore.RateLimiting;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.Options;
namespace EssentialCSharp.Web.Controllers;
public class HomeController(ILogger<HomeController> logger, IWebHostEnvironment hostingEnvironment, ISiteMappingService siteMappingService, ChapterDescriptionService chapterDescriptionService, IHttpContextAccessor httpContextAccessor, IRouteConfigurationService routeConfigurationService, IOptions<SiteSettings> siteSettings) : BaseController(routeConfigurationService, httpContextAccessor)
{
[EnableRateLimiting("content")]
public IActionResult Index()
{
string? key = Request.Path.Value?.TrimStart('/');
// if no key (default case), then load up home page
SiteMapping? siteMapping = siteMappingService.SiteMappings.Find(key);
if (string.IsNullOrEmpty(key))
{
return RedirectToAction(nameof(Home));
}
else if (siteMapping is not null)
{
string filePath = Path.Join(hostingEnvironment.ContentRootPath, Path.Join(siteMapping.PagePath));
HtmlDocument doc = new();
doc.Load(filePath);
// Generated chapter pages embed <style> blocks; allow them under the CSP via the request nonce.
if (CspNonce.Get(HttpContext) is string nonce)
{
foreach (HtmlNode styleNode in doc.DocumentNode.Descendants("style"))
{
styleNode.SetAttributeValue("nonce", nonce);
}
}
// The parser emits a canonical with a hardcoded production origin. Keep its path (so alias routes
// still point at the primary page) but re-base it on this deployment's configured origin, and
// remove it from the injected head so the layout renders exactly one canonical.
string canonicalPath = "/" + siteMapping.PrimaryKey;
foreach (HtmlNode canonicalNode in doc.DocumentNode.SelectNodes("/html/head/link[@rel='canonical']")?.ToList() ?? [])
{
string href = canonicalNode.GetAttributeValue("href", "");
if (Uri.TryCreate(href, UriKind.Absolute, out Uri? absolute))
{
canonicalPath = absolute.AbsolutePath;
}
else if (href.StartsWith('/'))
{
canonicalPath = href.Split('?', '#')[0];
}
canonicalNode.Remove();
}
string headHtml = doc.DocumentNode.Element("html")?.Element("head")?.InnerHtml ?? string.Empty;
string html = doc.DocumentNode.Element("html")?.Element("body")?.InnerHtml ?? string.Empty;
ViewBag.PageTitle = siteMapping.IndentLevel is 0 ? siteMapping.ChapterTitle + " " + siteMapping.RawHeading : siteMapping.RawHeading;
ViewData["Description"] = chapterDescriptionService.GetDescription(siteMapping);
ViewData["CanonicalUrl"] = $"{siteSettings.Value.BaseUrl.TrimEnd('/')}{Request.PathBase}{canonicalPath}";
ViewBag.NextPage = FlipPage(siteMapping!.ChapterNumber, siteMapping.PageNumber, true);
ViewBag.CurrentPageKey = siteMapping.PrimaryKey;
ViewBag.PreviousPage = FlipPage(siteMapping.ChapterNumber, siteMapping.PageNumber, false);
ViewBag.HeadContents = headHtml;
ViewBag.Contents = html;
ViewBag.NeedsPageHeading = doc.DocumentNode.SelectSingleNode("//body//h1") is null;
return View();
}
else
{
return RedirectToAction(nameof(Error), new { errorMessage = "Specified page not found, please check your spelling and try again", statusCode = 404 });
}
}
[Route("/TermsOfService",
Name = "TermsOfService")]
public IActionResult TermsOfService()
{
ViewBag.PageTitle = "Terms Of Service";
ViewData["Description"] = "Read the terms of service for using Essential C# and its online C# learning resources.";
return View();
}
[Route("/cookie-policy", Name = "CookiePolicy")]
public IActionResult CookiePolicy()
{
ViewBag.PageTitle = "Cookie Policy";
ViewData["Description"] = "Learn how Essential C# uses cookies and manages your privacy preferences.";
return View();
}
[Route("/Announcements", Name = "Announcements")]
public IActionResult Announcements()
{
ViewBag.PageTitle = "Announcements";
ViewData["Description"] = "Read the latest Essential C# announcements, updates, and news about the online C# guide.";
return View();
}
[Route("/about", Name = "about")]
public IActionResult About()
{
ViewBag.PageTitle = "About";
ViewData["Description"] = "Learn about Essential C#, the free online C# tutorial and reference by Mark Michaelis and Benjamin Michaelis.";
return View();
}
[Route("/home", Name = "home")]
public IActionResult Home()
{
ViewData["Description"] = "Explore Essential C#, a free online C# tutorial and reference with practical examples for developers.";
return View();
}
[Route("/guidelines", Name = "guidelines")]
public IActionResult Guidelines()
{
ViewBag.PageTitle = "Coding Guidelines";
ViewData["Description"] = "Explore Essential C# coding guidelines and best practices for writing clear, maintainable C#.";
FileInfo fileInfo = new(Path.Join(hostingEnvironment.ContentRootPath, "Guidelines", "guidelines.json"));
if (!fileInfo.Exists)
{
return RedirectToAction(nameof(Error), new { errorMessage = "Guidelines could not be found", statusCode = 404 });
}
ViewBag.Guidelines = fileInfo.ReadGuidelineJsonFromInputDirectory(logger);
ViewBag.GuidelinesUrl = Request.Path.Value;
return View();
}
[Route("/sitemap.xml")]
[OutputCache(Duration = 3600)]
[EnableRateLimiting("content")]
public IActionResult SitemapXml()
{
SitemapXmlHelpers.GenerateSitemapXml(siteMappingService.SiteMappings, RouteConfigurationService, siteSettings.Value.BaseUrl, out var nodes);
return new SitemapProvider().CreateSitemap(new SitemapModel(nodes));
}
private string FlipPage(int currentChapter, int currentPage, bool next)
{
if (siteMappingService.SiteMappings.Count == 0)
{
return "";
}
int page = -1;
if (next)
{
page = 1;
}
SiteMapping? siteMap = siteMappingService.SiteMappings.FirstOrDefault(f => f.ChapterNumber == currentChapter && f.PageNumber == currentPage + page);
if (siteMap is null)
{
if (next)
{
siteMap = siteMappingService.SiteMappings.FirstOrDefault(f => f.ChapterNumber == currentChapter + 1 && f.PageNumber == 1);
}
else
{
int? previousPage = siteMappingService.SiteMappings.LastOrDefault(f => f.ChapterNumber == currentChapter - 1)?.PageNumber;
siteMap = siteMappingService.SiteMappings.FirstOrDefault(f => f.ChapterNumber == currentChapter - 1 && f.PageNumber == previousPage);
}
if (siteMap is null)
{
return "";
}
}
return $"{siteMap.Keys.First()}#{siteMap.AnchorId}";
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error(string? errorMessage = null, int statusCode = 404)
{
if (statusCode is < 400 or > 599)
{
statusCode = 500;
}
Response.StatusCode = statusCode;
ViewBag.StatusCode = statusCode;
ViewBag.ErrorMessage = string.IsNullOrWhiteSpace(errorMessage) ? null : $"{statusCode}: {errorMessage}";
ViewBag.PageTitle = $"Error-{statusCode}";
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}