I am trying to implement the following custom error pages in ASP.NET Razor Pages (.Net 8.0):
My error pages are RazorPages pages within a directory called Error
with the Pages
directory. The Model code behind only has an empty constructor.
The application configuration is as below:
With the supplementary method as below to allow the 403 to be shown when the context is not fully started. Without this the standard ASPNet 403 page would show for non authorised page requests.
The order of setup, which I understand is critical, is what I have gleaned from MSDN and various Stack Overflow posts and solutions.
The 403 and 404 pages redirect and display PERFECTLY. The 500 page will not, no matter what I have tried from various Stack Overflow "Accepted" solutions. I just get the standard ASP.Net 500 page.
EDIT 1:
new NotImplementedException()
in the OnGetAsync()
action in the page code behind.app.Environment.IsDevelopment
switch disabled so it would UseExceptionHandler("/Error");
instead of the DeveloperExceptionPage.EDIT 2:
Brando suggested that I use app.UseExceptionHandler("/Error/500");
instead of app.UseExceptionHandler("/Error");
which I tried as below...
//if (app.Environment.IsDevelopment())
//{
// app.UseDeveloperExceptionPage();
//}
//else
//{
//app.UseExceptionHandler("/Error");
app.UseExceptionHandler("/Error/500");
app.UseHsts();
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
//}
...however, I am still not hitting my custom error page.
Is there something else I am missing?
Edit 3:
TLDR; The fix Brando suggested DID FIX the issue. Thank you Brando.
DETAIL;
I realised I had a random app.UseDeveloperExceptionPage();
still at the bottom of my WebApplicationConfiguration.cs
. Once this was removed, swicthing between my original code and Brando's code could introduce / fix the bug as Brando stated. Brando's suggestion fixed the issue.
Any help appreciated.
According to your codes, I still find you are using the previous error path inside the UseExceptionHandler method, but the 500 page's path should be error/500
I suggest you could try this to test if this will use the new error page:
app.UseExceptionHandler("/Error/500");
Result:
Path: