Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Hooks" in MVC3

I'm working on a MVC3 project using C#, I was wondering if MVC3 has something similar to hooks in CodeIgniter (for executing code just before each ActionResult executes). I need it to update an arraylist of visited websites in the session.

EDIT: I've worked out a solution using an ActionResult, I'll post it here for reference.

ActionFilter:

public class HistoryTracker : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // code here
    }
}

Global.asax.cs

 protected void Application_Start()
    {
        // ...
        GlobalFilters.Filters.Add(new HistoryTracker());
    }

This makes the ActionFilter always trigger.

like image 281
Geoffrey De Vylder Avatar asked Jan 20 '23 21:01

Geoffrey De Vylder


2 Answers

You are looking for ActionFilters.

like image 156
Femaref Avatar answered Jan 22 '23 10:01

Femaref


You should use ActionFilters. This's exactly what you need

like image 29
archil Avatar answered Jan 22 '23 11:01

archil