Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP module vs action filter in asp.net-mvc

I am developing an application in asp.net MVC3 and I have the following questions: When should I write an HTTP module and when should I write an action filter?

like image 872
Martin Avatar asked Jul 16 '12 15:07

Martin


3 Answers

  1. Filter are more MVC approach of doing thing whereas Http Module are more of ASP.NET way of doing thing. Both serve similar purpose by providing hook in the processing pipline.

  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.

like image 51
Anand Avatar answered Sep 29 '22 18:09

Anand


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.

Filters are designed to inject logic in between MVC request life cycle. Specifically before and after de 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

So if you want to intercept, participate, or modify in a specific of the 22 events in the http request pipeline choose the modules. If your logic is is strictly related to the action method you better server overriding one of the following ActionFilterAttribute methods:

  • OnActionExecuting
  • OnActionExecutted
  • OnResultExecuting
  • OnResultExecuted
like image 28
PedroSouki Avatar answered Sep 29 '22 16:09

PedroSouki


HttpModule is how IIS allows an Web application to override the default behavior or add custom logic by letting you attach event handlers to HttpApplication events. Different IIS modes (Integrated or Classic) even use has different Web.config settings.
Reference:
http://msdn.microsoft.com/en-us/library/ms227673(v=vs.100).aspx

Example: redirect non-www to www URLs

public void Init(HttpApplication application)
{
    application.PreRequestHandlerExecute += this.Application_PreRequestHandlerExecute;
}

private void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
    Uri requestUrl = HttpContext.Current.Request.Url;
    string host = requestUrl.Authority.ToLower();
    if (!host.StartsWith("www"))
    {
        HttpContext.Current.Response.Redirect(requestUrl.Scheme + "://www." + host + requestUrl.PathAndQuery);
        HttpContext.Current.Response.End();
    }
}

An Action Filter is an attribute decorating controllers or action methods. It is an abstraction layer between MVC routing and action methods. With action filters, we can apply same logic to multiple controllers or action methods. for example, custom logging.

like image 23
detale Avatar answered Sep 29 '22 16:09

detale