I would like to have 1 error page that depending on the query string provided displays a slightly different error message to the user.

I have noticed the following code in the the Startup.cs file when creating a new asp.net 5 project.

if (env.IsDevelopment())
{
    app.UseBrowserLink();
    app.UseDeveloperExceptionPage();
}
else
{
    app.UseExceptionHandler("/Home/Error");
}

I have been able to get this to display the correct error page when an exception occurs. My issue is that it seems to only catch errors that have not been handled in my application i.e. always with a status code of 500. Is this correct?

To handle 404 errors I am using the following code:

app.UseStatusCodePagesWithReExecute("/Error/{0}"); 

With my controller implemented as:

[HttpGet("{statusCode}")]
public IActionResult Error(int statusCode)
{
    return View(statusCode);
}

This seems to catch the 404 errors and displays the correct status code.

If I update my code in the above if statement to use the same action for example:

if (env.IsDevelopment())
{
    app.UseBrowserLink();
    app.UseDeveloperExceptionPage();
}
else
{
    app.UseExceptionHandler("/Error/{0}");
}

The status code returned is always 0.

In addition what will happen when a 400, 403 or any other occurs? Will they be caught? If so at what point will they be caught?

As you can tell I am very confused and would love for someone to provide me with an example where all the different status codes are handled.

Related posts

Recent Viewed