Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 3/4 HttpModule or ActionFilter

I need to check some stuff (Cookies) for each request coming to my application.

In ASP.NET we've used HttpModule for this task , the question what should be used in MVC ? Some Global Filter , or I can Use HttpModuler as well, is there Any difference in Request PipeLine between MVC and regular ASP.NET ?

like image 201
StringBuilder Avatar asked Nov 25 '12 11:11

StringBuilder


1 Answers

MVC is an abstraction over ASP.NET and therefore their "hooks" really depend at which level you want to inject your logic. An action filter will allow you to hook into MVC specific events:

  • OnActionExecuting – This method is called before a controller action is executed.
  • OnActionExecuted – This method is called after a controller action is executed.
  • OnResultExecuting – This method is called before a controller action result is executed.
  • OnResultExecuted – This method is called after a controller action result is executed.

Whereas an HttpModule only allows you to hook into ASP.NET (upon which MVC is built) specific events:

  • BeginRequest - Request has been started. If you need to do something at the beginning of a request (for example, display advertisement banners at the top of each page), synchronize this event.
  • AuthenticateRequest - If you want to plug in your own custom authentication scheme (for example, look up a user against a database to validate the password), build a module that synchronizes this event and authenticates the user in a way that you want to.
  • AuthorizeRequest - This event is used internally to implement authorization mechanisms (for example, to store your access control lists (ACLs) in a database rather than in the file system). Although you can override this event, there are not many good reasons to do so.
  • PreRequestHandlerExecute - This event occurs before the HTTP handler is executed.
  • PostRequestHandlerExecute - This event occurs after the HTTP handler is executed.
  • EndRequest - Request has been completed. You may want to build a debugging module that gathers information throughout the request and then writes the information to the page.

So it really depends on when you need to hook in your event and which events you need.

like image 107
armen.shimoon Avatar answered Oct 05 '22 22:10

armen.shimoon