---
title: "Discussion on \"Building a Global Error Handling Middleware in .NET Web API\" | Hashnode"
description: "Discussion on \"Building a Global Error Handling Middleware in .NET Web API\". Error handling is a critical aspect of web application development. Whether it's handling unexpected exceptions, validating user input, or dealing with various types of errors gracefully, a well-structured error handling mechanism is essential. In .N..."
url: "https://hashnode.com/posts/building-a-global-error-handling-middleware-in-net-web-api/65076e2141223ed714fac0bd"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Building a Global Error Handling Middleware in .NET Web API](https://memorycrypt.hashnode.dev/building-a-global-error-handling-middleware-in-net-web-api?utm_source=hashnode&utm_medium=feed)

Error handling is a critical aspect of web application development. Whether it's handling unexpected exceptions, validating user input, or dealing with various types of errors gracefully, a well-structured error handling mechanism is essential. In .N...

Sep 17, 2023 · 5 min read · memorycrypt.hashnode.dev  
Updated Sep 21, 2023  
Read the article

-   [#net](https://hashnode.com/tag/net)
-   [#exception-handling](https://hashnode.com/tag/exception-handling)
-   [#web-development](https://hashnode.com/tag/web-development)

## Written by

[Hemant Singh](https://hashnode.com/@daycoder)

@daycoder

  
[View profile](https://hashnode.com/@daycoder)

## Responses 2

Markdown supported · ⌘↵ to post

[Ajmal Muhammed](https://hashnode.com/@ajmalmuhammed)

[Sep 19, 2023](https://hashnode.com/posts/building-a-global-error-handling-middleware-in-net-web-api/65076e2141223ed714fac0bd/comment/650a21094a3e39112c026699)

Will invokeAsync take other arguments other than httpcontext? Like what if I want a custom middleware to be called next ? Can I pass that middleware into invokeAsync function?

[Hemant Singh](https://hashnode.com/@daycoder)

[Sep 21, 2023](https://hashnode.com/posts/building-a-global-error-handling-middleware-in-net-web-api/65076e2141223ed714fac0bd/comment/650a21094a3e39112c026699#reply-650bad1b8cd3ce237b6dd71d)

InvokeAsync can take any number parameters, as long as they are properly mapped using Dependency Injection. It is similar to a constructor in behavior that way. However, we should not clutter this method by injecting too many objects, unless they really add to the purpose of the middleware class.

We should not pass a middleware as one of the parameters in InvokeAsync, as you can easily define the sequence of calling middlewares in Startup or Program class. Right where we map our error handling middleware using app.UseMiddleware<ErrorHandlingMiddleware>(), we can write in the next line app.UseMiddleware<MyMiddleware>(), and that will ensure that MyMiddleware is next in line.

[Jubril Adebiyi](https://hashnode.com/@JubrilW2code)

[Sep 19, 2023](https://hashnode.com/posts/building-a-global-error-handling-middleware-in-net-web-api/65076e2141223ed714fac0bd/comment/6509f3554a3e39112c026397)

very insightful
