Skip to content

Commit 86a2171

Browse files
fix: use Keys.FirstOrDefault() instead of PrimaryKey (null after JSON deserialization)
SiteMapping.PrimaryKey is not serialized in sitemap.json; after deserialization it is null, causing ArgumentNullException in WordCountService constructor and HomeController when calling Dictionary.ContainsKey(null). Use mapping.Keys.FirstOrDefault() ?? mapping.PrimaryKey consistently, matching the pattern already used in BookToolQueryService. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 2e41f50 commit 86a2171

2 files changed

Lines changed: 7 additions & 6 deletions

File tree

‎EssentialCSharp.Web/Controllers/HomeController.cs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,17 @@ public IActionResult Index()
3434
string headHtml = doc.DocumentNode.Element("html")?.Element("head")?.InnerHtml ?? string.Empty;
3535
string html = doc.DocumentNode.Element("html")?.Element("body")?.InnerHtml ?? string.Empty;
3636

37+
string pageKey = siteMapping.Keys.FirstOrDefault() ?? siteMapping.PrimaryKey ?? string.Empty;
3738
ViewBag.PageTitle = siteMapping.IndentLevel is 0 ? siteMapping.ChapterTitle + " " + siteMapping.RawHeading : siteMapping.RawHeading;
3839
ViewBag.NextPage = FlipPage(siteMapping!.ChapterNumber, siteMapping.PageNumber, true);
39-
ViewBag.CurrentPageKey = siteMapping.PrimaryKey;
40+
ViewBag.CurrentPageKey = pageKey;
4041
ViewBag.PreviousPage = FlipPage(siteMapping.ChapterNumber, siteMapping.PageNumber, false);
4142
ViewBag.HeadContents = headHtml;
4243
ViewBag.Contents = html;
43-
ViewBag.PageWordCount = wordCountService.GetPageWordCount(siteMapping.PrimaryKey);
44+
ViewBag.PageWordCount = wordCountService.GetPageWordCount(pageKey);
4445
ViewBag.ChapterWordCount = wordCountService.GetChapterWordCount(siteMapping.ChapterNumber);
45-
ViewBag.WordsBeforePage = wordCountService.GetWordsBeforePage(siteMapping.PrimaryKey);
46-
ViewBag.ChapterStartWords = wordCountService.GetChapterStartWords(siteMapping.PrimaryKey);
46+
ViewBag.WordsBeforePage = wordCountService.GetWordsBeforePage(pageKey);
47+
ViewBag.ChapterStartWords = wordCountService.GetChapterStartWords(pageKey);
4748
ViewBag.BookWordCount = wordCountService.GetBookWordCount();
4849
return View();
4950
}

‎EssentialCSharp.Web/Services/WordCountService.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public WordCountService(ISiteMappingService siteMappingService, IWebHostEnvironm
4343

4444
foreach (SiteMapping mapping in orderedMappings)
4545
{
46-
string pageKey = mapping.PrimaryKey;
47-
if (_pageWordCounts.ContainsKey(pageKey))
46+
string? pageKey = mapping.Keys.FirstOrDefault() ?? mapping.PrimaryKey;
47+
if (pageKey is null || _pageWordCounts.ContainsKey(pageKey))
4848
{
4949
// Multiple anchors on the same page; skip duplicates (already counted).
5050
continue;

0 commit comments

Comments
 (0)