---
title: "Role of app.UseExceptionHandler in .Net core API?"  
description: "Role of app.UseExceptionHandler in .Net core API?"  
author: "Sandra Emily"  
published: 2023-08-28  
updated: 2023-08-29  
canonical: https://www.mindstick.com/forum/159709/role-of-app-useexceptionhandler-in-dot-net-core-api  
category: ".net core"  
tags: ["exception handling", ".net core", ".net core api"]  
reading_time: 2 minutes  

---

# Role of app.UseExceptionHandler in .Net core API?

[Role](https://yourviews.mindstick.com/audio/1254/the-role-of-visualization-in-achieving-your-goals) of app.UseExceptionHandler in .Net [core API](https://www.mindstick.com/forum/160547/how-to-pass-multiple-parameters-in-url-dot-net-core-api)?

## Replies

### Reply by Aryan Kumar

The `app.UseExceptionHandler()` method in ASP.NET Core [API](https://www.mindstick.com/articles/12641/instagram-api-upgraded-to-facebook-graph) is used to configure a custom error handling page. When an unhandled exception occurs in the application, the `UseExceptionHandler()` middleware will catch the exception and redirect the request to the custom error handling page.

The custom error handling page can be a static HTML page or a dynamic page generated by the application. The page should display the error message to the user and provide instructions on how to resolve the error.

The `app.UseExceptionHandler()` method takes two arguments:

- The first argument is the path to the custom error handling page.
- The second argument is an optional lambda expression that can be used to customize the error handling behavior.

The following code shows how to configure the `app.UseExceptionHandler()` middleware:

C#

```plaintext
app.UseExceptionHandler("/error");
```

This code will redirect all unhandled exceptions to the `/error` page.

The following code shows how to customize the error handling behavior using a lambda expression:

C#

```plaintext
app.UseExceptionHandler("/error", async (context, error) => {
  // Do some custom error handling here.
});
```

This code will first catch the unhandled exception and then perform some custom error handling. The custom error handling code can be used to display a different error message to the user, log the error to the application log, or take other actions.

The `app.UseExceptionHandler()` middleware is an important part of any ASP.NET Core API. It helps to ensure that unhandled exceptions are handled gracefully and that users are not presented with cryptic error messages.

Here are some additional things to keep in mind about the `app.UseExceptionHandler()` middleware:

- The `app.UseExceptionHandler()` middleware should be placed after the `app.UseRouting()` middleware.
- The `app.UseExceptionHandler()` middleware can be used in both the development and production environments. However, the error handling behavior may be different in each environment.
- The `app.UseExceptionHandler()` middleware can be used to handle all unhandled exceptions or only specific types of exceptions.


---

Original Source: https://www.mindstick.com/forum/159709/role-of-app-useexceptionhandler-in-dot-net-core-api

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
