======================Middleware vs filters========
The main difference between them is their scope. Filters are a part of MVC, so they are scoped entirely to the MVC middleware. Middleware only has access to the HttpContext and anything added by preceding middleware. In contrast, filters have access to the wider MVC context, so can access routing data and model binding information
for example.
Generally speaking, if you have a cross-cutting concern that is independent of MVC then using middleware makes sense, if your cross-cutting concern relies on MVC concepts, or must run midway through the MVC pipeline, then filters make sense.
==============================
ConsumesAttribute—Can be used to restrict the allowed formats an action method can accept. If your action is decorated with [Consumes("application/json")] but the client sends the request as XML, then the resource filter will short-circuit the pipeline and return a 415 Unsupported Media Type response.
==============================
Exception filters can catch exceptions from more than your action methods.
They’ll run if an exception occurs in MvcMiddleware
During model binding or validation
When the action method is executing
When an action filter is executing
You should note that exception filters won’t catch exceptions thrown in any filters
other than action filters, so it’s important your resource and result filters don’t throw
exceptions. Similarly, they won’t catch exceptions thrown when executing IActionResult itself.
================================
ProducesAttribute—This forces the Web API result to be serialized to a specific output format. For example, decorating your action method with [Produces("application/xml")] forces the formatters to try to format the response as XML, even if the client doesn’t list XML in its Accept header.
FormatFilterAttribute—Decorating an action method with this filter tells the formatter to look for a route value or query string parameter called format, and to use that to determine the output format. For example, you could call /api/recipe/11?format=json and FormatFilter will format the response as JSON, or call api/recipe/11?format=xml and get the response as XML.
======================================
As with resource and action filters, result filters can implement a method that runs after the result has been executed, OnResultExecuted. You can use this method, for example, to inspect exceptions that happened during the execution of IActionResult.
========================================
Generally, you can’t modify the response in the OnResultExecuted method, as MvcMiddleware may have already started streaming the response to the client.
========================================
The most interesting point here is that short-circuiting an action filter doesn’t shortcircuit much of the pipeline at all. In fact, it only bypasses later action filters and the action method execution itself. By primarily building action filters, you can ensure that other filters, such as result filters that define the output format, run as usual, even when your action filters short-circuit.
=======================================
The previous version of ASP.NET used filters, but they suffered from one problem in particular: it was hard to use services from them. This was a fundamental issue with implementing them as attributes that you decorate your actions with. C# attributes don’t let you pass dependencies into their constructors (other than constant values), and they’re created as singletons, so there’s only a single instance for the lifetime of your app.
========================================
the key is to split the filter into two. Instead of creating a class that’s both an attribute and a filter, create a filter class that contains the functionality and an attribute that tells MvcMiddleware when and where to use the filter.
a class implements IACtionFiler and Anoter class derived from TypeFilerAttribute, in this class we will pass type of IActionFiler implementing class so this will work as attribute.
===================================
Summary
The filter pipeline executes as part of MvcMiddleware after routing has selected an action method.
The filter pipeline consists of authorization filters, resource filters, action filters, exception filters, and Result filters. Each filter type is grouped into a stage.
Resource, action, and result filters run twice in the pipeline: an *Executing method on the way in and an *Executed method on the way out.
Authorization and exception filters only run once as part of the pipeline; they don’t run after a response has been generated.
Each type of filter has both a sync and an async version. For example, resource filters can implement either the IResourceFilter interface or the IAsyncResourceFilter interface. You should use the synchronous interface unless your filter needs to use asynchronous method calls.
You can add filters globally, at the controller level, or at the action level. This is called the scope of the filter. Within a given stage, global-scoped filters run first, then controller-scoped, and finally, action-scoped.
You can override the default order by implementing the IOrderedFilter interface. Filters will run from lowest to highest Order and use scope to break ties.
Authorization filters run first in the pipeline and control access to APIs. ASP.NET Core includes an [Authorization] attribute that you can apply to action methods so that only logged-in users can execute the action.
Resource filters run after authorization filters, and again after a result has been executed. They can be used to short-circuit the pipeline, so that an action
method is never executed. They can also be used to customize the model binding process for an action method.
Action filters run after model binding has occurred, just before an action method executes. They also run after the action method has executed. They can be used to extract common code out of an action method to prevent duplication.
The Controller base class also implements IActionFilter and IAsyncActionFilter. They run at the start and end of the action filter pipeline, regardless of the ordering or scope of other action filters.
Exception filters execute after action filters, when an action method has thrown an exception. They can be used to provide custom error handling specific to the action executed.
Generally, you should handle exceptions at the middleware level, but exception filters let you customize how you handle exceptions for specific actions or controllers.
Result filters run just before and after an IActionResult is executed. You can use them to control how the action result is executed, or to completely change the action result that will be executed.
You can use ServiceFilterAttribute and TypeFilterAttribute to allow dependency injection in your custom filters. ServiceFilterAttribute requires that you register your filter and all its dependencies with the DI container, whereas TypeFilterAttribute only requires that the filter’s dependencies have been registered