concept exception in category asp.net

This is an excerpt from Manning's book ASP.NET Core in Action, Second Edition.
Kestrel is responsible for receiving the request data and constructing a C# representation of the request, but it doesn’t attempt to generate a response directly. For that, Kestrel hands the
HttpContextto the middleware pipeline found in every ASP.NET Core application. This is a series of components that processes the incoming request to perform common operations such as logging, handling exceptions, or serving static files.
Exceptions typically occur whenever you find an unexpected circumstance. A typical (and highly frustrating) exception you’ll no doubt have experienced before is
NullReferenceException, which is thrown when you attempt to access an object that hasn’t been initialized[1]. If an exception occurs in a middleware component, it propagates up the pipeline, as shown in figure 3.13. If the pipeline doesn’t handle the exception, the web server will return a 500 status code back to the user.Figure 3.13 An exception in the endpoint middleware propagates through the pipeline. If the exception isn’t caught by middleware earlier in the pipeline, then a 500 “Server error” status code is sent to the user’s browser.
![]()
In some situations, an error won’t cause an exception. Instead, middleware might generate an error status code. One such case you’ve already seen is when a requested path isn’t handled. In that situation, the pipeline will return a 404 error, which results in a generic, unfriendly page being shown to the user, as you saw in figure 3.7. Although this behavior is “correct,” it doesn’t provide a great experience for users of your application.
ExceptionHandlerMiddlewarewill invoke this path after it captures an exception, in order to generate the final response. The ability to dynamically generate a response is a key feature ofExceptionHandlerMiddleware—it allows you to re-execute a middleware pipeline in order to generate the response sent to the user.Figure 3.17 shows what happens when
ExceptionHandlerMiddlewarehandles an exception. It shows the flow of events when the Index.chstml Razor Page generates an exception when a request is made to the"/"path. The final response returns an error status code but also provides an HTML response to display to the user, using the"/Error"path.
Figure 3.17 ExceptionHandlerMiddleware handling an exception to generate an HTML response. A request to the / path generates an exception, which is handled by the middleware. The pipeline is re-executed using the /Error path to generate the HTML response.The sequence of events when an exception occurs somewhere in the middleware pipeline after
ExceptionHandlerMiddlewareis as follows:A piece of middleware throws an exception. ExceptionHandlerMiddlewarecatches the exception.
ExceptionHandlerMiddlewareandDeveloperExceptionPageMiddlewareare great for catching exceptions in your application, but exceptions aren’t the only sort of errors you’ll encounter. In some cases, your middleware pipeline will return an HTTP error status code in the response. It’s important to handle both exceptions and error status codes to provide a coherent user experience.

This is an excerpt from Manning's book ASP.NET Core in Action.
In development, DeveloperExceptionPageMiddleware (added by the UseDeveloperExceptionPage() call) ensures that, if your application throws an exception that isn’t caught, you’ll be presented with as much information as possible in the browser to diagnose the problem, as shown in figure 2.10. It’s akin to the “yellow screen of death” in the previous version of ASP.NET, but this time it’s white, not yellow!
ExceptionHandlerMiddleware will invoke this path after it captures an exception, in order to generate the final response. The ability to dynamically generate a response is a key feature of ExceptionHandlerMiddleware—it allows you to re-execute a middleware pipeline in order to generate the response sent to the user.
Figure 3.16 shows what happens when ExceptionHandlerMiddleware handles an exception. It shows the flow of events when the MVC middleware generates an exception when a request is made to the /home path. The final response returns an error status code but also provides an HTML response to display to the user, using the /error path.
Figure 3.16. ExceptionHandlerMiddleware handling an exception to generate an HTML response. A request to the /Home path generates an exception, which is handled by the middleware. The pipeline is re-executed using the /Error path to generate the HTML response.
![]()
The sequence of events when an exception occurs somewhere in the middleware pipeline after ExceptionHandlerMiddleware is as follows:
1. A piece of middleware throws an exception.
2. ExceptionHandlerMiddleware catches the exception.
ExceptionHandlerMiddleware and DeveloperExceptionPageMiddleware are great for catching exceptions in your application, but exceptions aren’t the only sort of errors you’ll encounter. In some cases, your middleware pipeline will return an HTTP error status code in the response. It’s important to handle both exceptions and error status codes to provide a coherent user experience.
Listing 13.6. Recipe Web API controller before refactoring to use filters
[Route("api/recipe")] public class RecipeApiController : Controller { private const bool IsEnabled = true; #1 public RecipeService _service; public RecipeApiController(RecipeService service) { _service = service; } [HttpGet("{id}")] public IActionResult Get(int id) { if (!IsEnabled) { return BadRequest(); } #2 try { if (!_service.DoesRecipeExist(id)) #3 { #3 return NotFound(); #3 } #3 var detail = _service.GetRecipeDetail(id); #4 Response.GetTypedHeaders().LastModified = #5 detail.LastModified; #5 return Ok(detail); #6 } catch (Exception ex) #7 { #7 return GetErrorResponse(ex); #7 } #7 } [HttpPost("{id}")] public IActionResult Edit( int id, [FromBody] UpdateRecipeCommand command) { if (!IsEnabled) { return BadRequest(); } #8 try { if (!ModelState.IsValid) #9 { #9 return BadRequest(ModelState); #9 } #9 if (!_service.DoesRecipeExist(id)) #10 { #10 return NotFound(); #10 } #10 _service.UpdateRecipe(command); #11 return Ok(); #11 } catch (Exception ex) #12 { #12 return GetErrorResponse(ex); #12 } #12 } private static IActionResult GetErrorResponse(Exception ex) { var error = new { Success = false, Errors = new[] { ex.Message } }; return new ObjectResult(error) { StatusCode = 500 }; } } #1 These fields would be passed in as configuration values and are used to control access to actions. #2 If the API isn’t enabled, block further execution. #3 If the requested Recipe doesn’t exist, return a 404 response. #4 Fetch RecipeDetailViewModel. #5 Sets the Last-Modified response header to the value in the model #6 Returns the view model with a 200 response #7 If an exception occurs, catch it, and return the error in an expected format, as a 500 error. #8 If the API isn’t enabled, block further execution. #9 Validates the binding model and returns a 400 response if there are errors #10 If the requested Recipe doesn’t exist, return a 404 response. #11 Updates the Recipe from the command and returns a 200 response #12 If an exception occurs, catch it, and return the error in an expected format, as a 500 error. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":3,\"ch\":63},{\"line\":3,\"ch\":65}],[{\"line\":48,\"ch\":63},{\"line\":48,\"ch\":65}],[{\"line\":49,\"ch\":63},{\"line\":49,\"ch\":65}],[{\"line\":50,\"ch\":63},{\"line\":50,\"ch\":65}],[{\"line\":51,\"ch\":63},{\"line\":51,\"ch\":65}],[{\"line\":53,\"ch\":63},{\"line\":53,\"ch\":65}],[{\"line\":54,\"ch\":63},{\"line\":54,\"ch\":65}],[{\"line\":56,\"ch\":63},{\"line\":56,\"ch\":65}],[{\"line\":57,\"ch\":63},{\"line\":57,\"ch\":65}],[{\"line\":58,\"ch\":63},{\"line\":58,\"ch\":65}],[{\"line\":59,\"ch\":63},{\"line\":59,\"ch\":65}],[{\"line\":80,\"ch\":0},{\"line\":80,\"ch\":2}],[{\"line\":89,\"ch\":0},{\"line\":89,\"ch\":2}],[{\"line\":90,\"ch\":0},{\"line\":90,\"ch\":2}],[{\"line\":91,\"ch\":0},{\"line\":91,\"ch\":2}],[{\"line\":13,\"ch\":63},{\"line\":13,\"ch\":65}],[{\"line\":81,\"ch\":0},{\"line\":81,\"ch\":2}],[{\"line\":17,\"ch\":63},{\"line\":17,\"ch\":65}],[{\"line\":18,\"ch\":63},{\"line\":18,\"ch\":65}],[{\"line\":19,\"ch\":63},{\"line\":19,\"ch\":65}],[{\"line\":20,\"ch\":63},{\"line\":20,\"ch\":65}],[{\"line\":82,\"ch\":0},{\"line\":82,\"ch\":2}],[{\"line\":17,\"ch\":63},{\"line\":17,\"ch\":65}],[{\"line\":18,\"ch\":63},{\"line\":18,\"ch\":65}],[{\"line\":19,\"ch\":63},{\"line\":19,\"ch\":65}],[{\"line\":20,\"ch\":63},{\"line\":20,\"ch\":65}],[{\"line\":82,\"ch\":0},{\"line\":82,\"ch\":2}],[{\"line\":17,\"ch\":63},{\"line\":17,\"ch\":65}],[{\"line\":18,\"ch\":63},{\"line\":18,\"ch\":65}],[{\"line\":19,\"ch\":63},{\"line\":19,\"ch\":65}],[{\"line\":20,\"ch\":63},{\"line\":20,\"ch\":65}],[{\"line\":82,\"ch\":0},{\"line\":82,\"ch\":2}],[{\"line\":17,\"ch\":63},{\"line\":17,\"ch\":65}],[{\"line\":18,\"ch\":63},{\"line\":18,\"ch\":65}],[{\"line\":19,\"ch\":63},{\"line\":19,\"ch\":65}],[{\"line\":20,\"ch\":63},{\"line\":20,\"ch\":65}],[{\"line\":82,\"ch\":0},{\"line\":82,\"ch\":2}],[{\"line\":22,\"ch\":63},{\"line\":22,\"ch\":65}],[{\"line\":83,\"ch\":0},{\"line\":83,\"ch\":2}],[{\"line\":24,\"ch\":63},{\"line\":24,\"ch\":65}],[{\"line\":25,\"ch\":63},{\"line\":25,\"ch\":65}],[{\"line\":84,\"ch\":0},{\"line\":84,\"ch\":2}],[{\"line\":24,\"ch\":63},{\"line\":24,\"ch\":65}],[{\"line\":25,\"ch\":63},{\"line\":25,\"ch\":65}],[{\"line\":84,\"ch\":0},{\"line\":84,\"ch\":2}],[{\"line\":27,\"ch\":63},{\"line\":27,\"ch\":65}],[{\"line\":85,\"ch\":0},{\"line\":85,\"ch\":2}],[{\"line\":29,\"ch\":63},{\"line\":29,\"ch\":65}],[{\"line\":30,\"ch\":63},{\"line\":30,\"ch\":65}],[{\"line\":31,\"ch\":63},{\"line\":31,\"ch\":65}],[{\"line\":32,\"ch\":63},{\"line\":32,\"ch\":65}],[{\"line\":86,\"ch\":0},{\"line\":86,\"ch\":2}],[{\"line\":29,\"ch\":63},{\"line\":29,\"ch\":65}],[{\"line\":30,\"ch\":63},{\"line\":30,\"ch\":65}],[{\"line\":31,\"ch\":63},{\"line\":31,\"ch\":65}],[{\"line\":32,\"ch\":63},{\"line\":32,\"ch\":65}],[{\"line\":86,\"ch\":0},{\"line\":86,\"ch\":2}],[{\"line\":29,\"ch\":63},{\"line\":29,\"ch\":65}],[{\"line\":30,\"ch\":63},{\"line\":30,\"ch\":65}],[{\"line\":31,\"ch\":63},{\"line\":31,\"ch\":65}],[{\"line\":32,\"ch\":63},{\"line\":32,\"ch\":65}],[{\"line\":86,\"ch\":0},{\"line\":86,\"ch\":2}],[{\"line\":29,\"ch\":63},{\"line\":29,\"ch\":65}],[{\"line\":30,\"ch\":63},{\"line\":30,\"ch\":65}],[{\"line\":31,\"ch\":63},{\"line\":31,\"ch\":65}],[{\"line\":32,\"ch\":63},{\"line\":32,\"ch\":65}],[{\"line\":86,\"ch\":0},{\"line\":86,\"ch\":2}],[{\"line\":39,\"ch\":63},{\"line\":39,\"ch\":65}],[{\"line\":87,\"ch\":0},{\"line\":87,\"ch\":2}],[{\"line\":43,\"ch\":63},{\"line\":43,\"ch\":65}],[{\"line\":44,\"ch\":63},{\"line\":44,\"ch\":65}],[{\"line\":45,\"ch\":63},{\"line\":45,\"ch\":65}],[{\"line\":46,\"ch\":63},{\"line\":46,\"ch\":65}],[{\"line\":88,\"ch\":0},{\"line\":88,\"ch\":2}],[{\"line\":43,\"ch\":63},{\"line\":43,\"ch\":65}],[{\"line\":44,\"ch\":63},{\"line\":44,\"ch\":65}],[{\"line\":45,\"ch\":63},{\"line\":45,\"ch\":65}],[{\"line\":46,\"ch\":63},{\"line\":46,\"ch\":65}],[{\"line\":88,\"ch\":0},{\"line\":88,\"ch\":2}],[{\"line\":43,\"ch\":63},{\"line\":43,\"ch\":65}],[{\"line\":44,\"ch\":63},{\"line\":44,\"ch\":65}],[{\"line\":45,\"ch\":63},{\"line\":45,\"ch\":65}],[{\"line\":46,\"ch\":63},{\"line\":46,\"ch\":65}],[{\"line\":88,\"ch\":0},{\"line\":88,\"ch\":2}],[{\"line\":43,\"ch\":63},{\"line\":43,\"ch\":65}],[{\"line\":44,\"ch\":63},{\"line\":44,\"ch\":65}],[{\"line\":45,\"ch\":63},{\"line\":45,\"ch\":65}],[{\"line\":46,\"ch\":63},{\"line\":46,\"ch\":65}],[{\"line\":88,\"ch\":0},{\"line\":88,\"ch\":2}],[{\"line\":48,\"ch\":63},{\"line\":48,\"ch\":66}],[{\"line\":49,\"ch\":63},{\"line\":49,\"ch\":66}],[{\"line\":50,\"ch\":63},{\"line\":50,\"ch\":66}],[{\"line\":51,\"ch\":63},{\"line\":51,\"ch\":66}],[{\"line\":89,\"ch\":0},{\"line\":89,\"ch\":3}],[{\"line\":48,\"ch\":63},{\"line\":48,\"ch\":66}],[{\"line\":49,\"ch\":63},{\"line\":49,\"ch\":66}],[{\"line\":50,\"ch\":63},{\"line\":50,\"ch\":66}],[{\"line\":51,\"ch\":63},{\"line\":51,\"ch\":66}],[{\"line\":89,\"ch\":0},{\"line\":89,\"ch\":3}],[{\"line\":48,\"ch\":63},{\"line\":48,\"ch\":66}],[{\"line\":49,\"ch\":63},{\"line\":49,\"ch\":66}],[{\"line\":50,\"ch\":63},{\"line\":50,\"ch\":66}],[{\"line\":51,\"ch\":63},{\"line\":51,\"ch\":66}],[{\"line\":89,\"ch\":0},{\"line\":89,\"ch\":3}],[{\"line\":48,\"ch\":63},{\"line\":48,\"ch\":66}],[{\"line\":49,\"ch\":63},{\"line\":49,\"ch\":66}],[{\"line\":50,\"ch\":63},{\"line\":50,\"ch\":66}],[{\"line\":51,\"ch\":63},{\"line\":51,\"ch\":66}],[{\"line\":89,\"ch\":0},{\"line\":89,\"ch\":3}],[{\"line\":53,\"ch\":63},{\"line\":53,\"ch\":66}],[{\"line\":54,\"ch\":63},{\"line\":54,\"ch\":66}],[{\"line\":90,\"ch\":0},{\"line\":90,\"ch\":3}],[{\"line\":53,\"ch\":63},{\"line\":53,\"ch\":66}],[{\"line\":54,\"ch\":63},{\"line\":54,\"ch\":66}],[{\"line\":90,\"ch\":0},{\"line\":90,\"ch\":3}],[{\"line\":56,\"ch\":63},{\"line\":56,\"ch\":66}],[{\"line\":57,\"ch\":63},{\"line\":57,\"ch\":66}],[{\"line\":58,\"ch\":63},{\"line\":58,\"ch\":66}],[{\"line\":59,\"ch\":63},{\"line\":59,\"ch\":66}],[{\"line\":91,\"ch\":0},{\"line\":91,\"ch\":3}],[{\"line\":56,\"ch\":63},{\"line\":56,\"ch\":66}],[{\"line\":57,\"ch\":63},{\"line\":57,\"ch\":66}],[{\"line\":58,\"ch\":63},{\"line\":58,\"ch\":66}],[{\"line\":59,\"ch\":63},{\"line\":59,\"ch\":66}],[{\"line\":91,\"ch\":0},{\"line\":91,\"ch\":3}],[{\"line\":56,\"ch\":63},{\"line\":56,\"ch\":66}],[{\"line\":57,\"ch\":63},{\"line\":57,\"ch\":66}],[{\"line\":58,\"ch\":63},{\"line\":58,\"ch\":66}],[{\"line\":59,\"ch\":63},{\"line\":59,\"ch\":66}],[{\"line\":91,\"ch\":0},{\"line\":91,\"ch\":3}],[{\"line\":56,\"ch\":63},{\"line\":56,\"ch\":66}],[{\"line\":57,\"ch\":63},{\"line\":57,\"ch\":66}],[{\"line\":58,\"ch\":63},{\"line\":58,\"ch\":66}],[{\"line\":59,\"ch\":63},{\"line\":59,\"ch\":66}],[{\"line\":91,\"ch\":0},{\"line\":91,\"ch\":3}]]"} !@%STYLE%@!