I am a beginner on Razor pages and trying to Re-direct all bad requests to a custom error page but it keeps giving me 500 error. I am using this document as reference.
https://www.learnrazorpages.com/configuration/custom-errors
this is what I tried:
- On my
Startup.csI have the following:
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
//app.UseExceptionHandler("/Error/{0}");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
app.UseStatusCodePagesWithRedirects("/Error/{0}");
}
- My
Error.cshtmlpage looks like this:
@page "{id}"
@model ErrorModel
@{
ViewData["Title"] = "Error";
}
- My
Error.cshtml.cslook like this:
public IActionResult OnGet(int id)
{
return Page();
}
public IActionResult OnPost(int id)
{
return Page();
}
public void OnGet()
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
}
but no matter what invalid URL I type in the browser it seems to re-direct to the custom page but looks like I get 500 error. Can someone point me on what I am doing wrong.
thank you!

