HttpModule vs Action Filter in asp.net mvc - ablealias/MVC GitHub Wiki

  1. Filters are more MVC approach of doing thing whereas Http Module is more of ASP.NET way of doing things. Both serve the similar purpose by providing hook in the processing pipeline.
  2. HttpModule is more generic and when you want some thing to be processed on every request. Filters are useful for adding action specific behaviour.
  3. If you want some thing to be executed only once per Http Request, you should use an HttpModule. ActionFilter may get executed several times during a request until and unless you check IsChildActionOn.

HttpModule are called before and after the request handler executes. They are intended to enable a developer to intercept, participate, or modify each request. There are 22 available events that can be subscribed to that enables the module to work on the request in various stages of the process. The events are useful for page developers who want to run code when key request pipeline events are raised. They are also useful if you are developing a custom module and you want the module to be invoked for all requests to the pipeline.

Since the HttpModule executes in each and every request, we can implement such logic/checking that needs to be performed for each and every HTTP request or response. For example.

  • To check request type
  • To perform security checking
  • To log incoming requests from a different location for business needs

Filters are designed to inject logic in between MVC request life cycle. Specifically before and after the action is invoked, as well as, before and after the result is processed. Filters provide users with powerful ways to inspect, analyze, capture and instruments several things going around within MVC projects. As of MVC5, there are 5 types of filters :

  • Authentication
  • Authorization
  • Action
  • Result
  • Exception