0
votes

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:

  1. On my Startup.cs I 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}");
}
  1. My Error.cshtml page looks like this:
@page "{id}"
@model ErrorModel
@{
    ViewData["Title"] = "Error";
}
  1. My Error.cshtml.cs look 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!

enter image description here

1
500 means internal server error, so you need to debug the application - vivek nuna
The url has 404 in it. 404 is not a page name it is a status code. Make a page named Error and then try to navigate. - noobprogrammer

1 Answers

1
votes

You can change the UseStatusCodePagesWithRedirects with UseStatusCodePagesWithReExecute and pass ?code={0} as query format.

if (env.IsDevelopment())
{
    app.UseStatusCodePagesWithReExecute("/Error", "?code={0}");
    //app.UseDeveloperExceptionPage();
}
{
    //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.UseStatusCodePagesWithReExecute("/Error", "?code={0}");
}

and show status code in view like this

Error.cshtml.cs

Add int? code in OnGet method

public int Code { get; set; }
//public IActionResult OnGet(int id)
//{
//    return Page();
//}
public void OnGet(int? code)
{
    Code = code ?? 0;
    RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
}

Error.cshtml

Add @page "{code?}" and <p>Error Code : @Model.Code </p>

@page "{code?}"
@model ErrorModel
@{
    ViewData["Title"] = "Error";
}

<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>
<p>Error Code : @Model.Code </p>
@if (Model.ShowRequestId)
{
    <p>
        <strong>Request ID:</strong> <code>@Model.RequestId</code>
    </p>
}

<h3>Development Mode</h3>
<p>
    Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
</p>
<p>
    <strong>The Development environment shouldn't be enabled for deployed applications.</strong>
    It can result in displaying sensitive information from exceptions to end users.
    For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
    and restarting the app.
</p>

Result enter image description here