MVC 3/4 HttpModule or ActionFilter

I need to check some cookies for every request coming into my application.

In ASP.NET we used the HttpModule for this task, the question is, what should be used in MVC? Some Global filter, or I can use HttpModuler, and is there also a difference in Request PipeLine between MVC and regular ASP.NET?

+7
source share
3 answers

MVC is an abstraction of ASP.NET, and therefore their “hooks” really depend on what level you want to introduce your logic into. An action filter allows you to connect to specific MVC events:

  • OnActionExecuting - This method is called before the controller action is performed.
  • OnActionExecuted - This method is called after the controller action is completed.
  • OnResultExecuting - this method is called before the result of the controller action is executed.
  • OnResultExecuted - this method is called after the result of the controller action.

While the HttpModule allows you to connect to ASP.NET (through which MVC is created) certain events:

  • BeginRequest - Request started. If you need to do something at the beginning of the request (for example, display advertising banners at the top of each page), synchronize this event.
  • AuthenticateRequest . If you want to connect your own authentication scheme (for example, search for a user in the database to verify the password), create a module that synchronizes this event and authenticates the user in such a way that you want.
  • AuthorizeRequest - this event is used internally to implement authorization mechanisms (for example, to store access control lists (ACLs) in the database, and not in the file system). Although you can redefine this event, there are not many good reasons for this.
  • PreRequestHandlerExecute - This event occurs before the HTTP handler executes.
  • PostRequestHandlerExecute - This event occurs after the execution of the HTTP handler.
  • EndRequest - Request completed. You might want to create a debugging module that collects information for the entire request and then writes information to the page.

So it really depends on when you need to connect to your event and what events you need.

+11
source

If the HttpModule worked well for you before then, it will continue to work with Mvc.

The other parts of your question are quite broad in scope and suggest that you would have read a good article on the asp.net-mvc and extensibilit pipeline .

+3
source

I did similar things using a global action filter. It works very well and your code is integrated into your application.

The HTTP module also works, of course, but that would mean splitting the code from the main application and maintaining it separately. If your code does not span multiple sites or is used in multiple applications or should work with web form sites, I would use a global filter.

+2
source

All Articles