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.
You can handle default errors at the application level either by modifying your application's configuration or by adding an Application_Error handler in the Global. asax file of your application. You can handle default errors and HTTP errors by adding a customErrors section to the Web. config file.
Is the framework outdated? ASP.NET MVC is no longer in active development.
In ASP.NET MVC we may have three ways to handle exceptions, Try-catch-finally. Exception filter. Application_Error event.
It sounds like you're confusing unhandled exceptions (which are, by default, returned to the client as an HTTP 500 Internal Server Error) and correctly-handled error cases caused by invalid action on behalf of the user/client (where a 4xx HTTP code is returned to the user).
Only the former of these has anything to do with the UseExceptionHandler call - by default it will catch any unhandled exceptions and route them to whatever you provide (in your case, a view, but it could just as easily be a piece of code which inspects the unhandled exceptions to convert certain error cases into HTTP 4xx return codes - for example, authentication errors into HTTP 401 responses).
UseStatusCodePagesWithReExecute will step in where a status code has been generated of 400-599, so long as no response body has been generated already. The source code in question shows how this is determined.
In your second code block, you've used UseExceptionHandler - I think you should have the following:
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
// Handle unhandled errors
app.UseExceptionHandler("/Home/Error");
// Display friendly error pages for any non-success case
// This will handle any situation where a status code is >= 400
// and < 600, so long as no response body has already been
// generated.
app.UseStatusCodePagesWithReExecute("/Error/{0}");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With