Skip to content

Commit ccc1499

Browse files
Back button error page
1 parent da0e307 commit ccc1499

3 files changed

Lines changed: 29 additions & 9 deletions

File tree

EssentialCSharp.Web/Controllers/HomeController.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,13 @@ private string FlipPage(int currentChapter, int currentPage, bool next)
121121
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
122122
public IActionResult Error(string? errorMessage = null, int statusCode = 404)
123123
{
124+
if (statusCode is < 400 or > 599)
125+
{
126+
statusCode = 500;
127+
}
124128
Response.StatusCode = statusCode;
125-
ViewBag.ErrorMessage = $"{statusCode}: {errorMessage}";
129+
ViewBag.StatusCode = statusCode;
130+
ViewBag.ErrorMessage = string.IsNullOrWhiteSpace(errorMessage) ? null : $"{statusCode}: {errorMessage}";
126131
ViewBag.PageTitle = $"Error-{statusCode}";
127132
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
128133
}
Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
1-
<div class="py-2 px-2">
2-
<h1>Error</h1>
3-
<div>
4-
@ViewBag.ErrorMessage
1+
@{
2+
int statusCode = ViewBag.StatusCode ?? 400;
3+
string heading = statusCode switch
4+
{
5+
404 => "Page Not Found",
6+
500 => "Something Went Wrong",
7+
_ => "Error"
8+
};
9+
}
10+
11+
<div class="py-2 px-2">
12+
<h1>@heading</h1>
13+
@if (ViewBag.ErrorMessage is string msg)
14+
{
15+
<div class="mb-3">@msg</div>
16+
}
17+
<div class="d-flex gap-2">
18+
<a href="/home" class="btn btn-primary">Go Home</a>
19+
<button type="button" class="btn btn-secondary"
20+
onclick="if(history.length > 1){ history.back(); } else { window.location='/home'; }">
21+
Go Back
22+
</button>
523
</div>
624
</div>

EssentialCSharp.Web/wwwroot/js/site.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,7 @@ const app = createApp({
294294
return tocData.filter(item => filterItem(item, query));
295295
});
296296

297-
const isContentPage = computed(() => {
298-
let path = window.location.pathname;
299-
return path !== '/home' && path !== '/guidelines' && path !== '/about' && path !== '/announcements';
300-
});
297+
const isContentPage = computed(() => percentComplete.value !== null);
301298

302299
function filterItem(item, query) {
303300
let matches = normalizeString(item.title).includes(query);

0 commit comments

Comments
 (0)