简体   繁体   中英

Confused with error handling in ASP.net 5 MVC 6

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 ie 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.

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}"); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM