I wrote a action filter which is similar to a log function :
public class TrackActionFilterAttribute : ActionFilterAttribute, IActionFilter
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
Tracking track = new Tracking()
{
Action = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName + filterContext.ActionDescriptor.ActionName
};
context.Trackings.Add(track);
context.SaveChanges();
}
}
and I have action methods like this :
[TrackActionFilter]
public ActionResult Index()
{
if (Request.Form["registered"] == 1)
return PartialView("Landing");
else
return PartialView("Register");
}
[TrackActionFilter]
public ActionResult Landing()
{
return PartialView();
}
[TrackActionFilter]
public ActionResult Register()
{
return PartialView();
}
My problem is when the Index action gets called, I am only registering the Index Action into the database. Since navigating to Index will result in either the Landing or Register Partial View, is there anyway to get these actions in any type of action filters?
The main reason is because i also want to track these actions inside my database and I cannot seem to do that. I’ve tried ActionExecuted , ResultExecuted to no avail. Would be glad if any experts could help!
You have access request parameters in your action filter:
and if you want to know which partial view was returned you have to use
OnActionExecutedevent because this event runs after the controller action has finished: