HTTP module vs action filter in asp.net-mvc
Asked Answered
U

3

20

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?

Ubana answered 16/7, 2012 at 15:29 Comment(0)
U
32
  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.

Uralian answered 16/7, 2012 at 16:9 Comment(0)
M
9

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
Merriman answered 21/10, 2016 at 18:12 Comment(1)
I know that the OP didn't ask about "global action filters" (the ones we plug in server-wide config files during startup). Would you still be kind enough to make an explicit "honorable mention" about when to use global-action filters? I was always under the impression that I could modify http responses just fine via global-action filters when it came to handling of exceptions stemming from the db-layer. Is it recommended to employ an http-module for this sort of thing? Thank you in advance.Reaves
P
1

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.

Penis answered 1/8, 2013 at 3:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.