Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExceptionHandling Middleware vs filter aspnetcore webapi 2.1

I wonder if somebody can clarify this one .I find the usage confusing.

Both links and video did NOT answer my question

I am aware of links like asp.net core middleware vs filters

and even a video about it

However for the purpose of the webApi alone does it make sense to implement both?

My understanding is that they cover different parts of the system.

Is there a project (github)or link that clearly explain how to use them.

Can I use both in the same webApi ?

Any samples?

thanks

like image 643
developer9969 Avatar asked Jun 16 '18 11:06

developer9969


People also ask

What is difference between middleware and filter?

Middlewares run on every request, regardless of which controller or action is called. Filters have full access to the MVC context , meaning that we have access to the: routing data, current controller, ModelState, etc. For example, we could create a filter that validates the input model.

What is exception handling middleware?

The exception handling middleware re-executes the request using the original HTTP method. If an error handler endpoint is restricted to a specific set of HTTP methods, it runs only for those HTTP methods. For example, an MVC controller action that uses the [HttpGet] attribute runs only for GET requests.

What is middleware in Web API?

Middleware is software that's assembled into an app pipeline to handle requests and responses. Each component: Chooses whether to pass the request to the next component in the pipeline. Can perform work before and after the next component in the pipeline.

What are the filters in Web API?

Web API includes filters to add extra logic before or after action method executes. Filters can be used to provide cross-cutting features such as logging, exception handling, performance measurement, authentication and authorization.


1 Answers

You can use both in your project but if your project only has MVC APIs and you are concerned only about capturing unhandled exceptions from your code then using a filter or middleware won't make much of difference and I would say use a filter.

However, if you are more concerned about errors that may occur outside of the MVC context or your code, for example, you want to capture an error in the Routing middleware then use a middleware.

Another major reason to use a filter would be that you get the MVC context in the filter. So if you have a logic like that if the exception occurred when the call was made to POST /orders then you want to log a different exception and if it was in another action then do something else. In such a case use a Filter.

In summary, I try to follow a flow like this

AM I CONCERNED ABOUT ERRORS IN THE ASP.NET FRAMEWORK - Use Middleware

AM I CONCERNED ONLY ABOUT ERRORS IN MY CODE - Use Filter

DO I NEED TO CAPTURE THE GLOBAL ERRORS IN THE ASP.NET FRAMEWORK AS WELL AS I HAVE UNIQUE LOGIC BASED ON WHERE THE ERROR OCCURRED - Use Both

like image 101
Pratik Bhattacharya Avatar answered Nov 13 '22 12:11

Pratik Bhattacharya