If you want to log every action execution in the ASP.NET MVC application.
Instead of adding unnecessary logic ( to logging ) in each action. We can override method OnActionExecuting which belongs to the Contoller class. It is launched every time we enter the action.
Example.
protected override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); if (RouteData.Values.TryGetValue("controller", out var controller)) { var action = default(object); if (RouteData.Values.TryGetValue("action", out action)) { var actionLog = action.ToString(); var contollerLog = controller.ToString(); } } }
In that way we can do whatever we want with information about what action in the controller was clicked from. If we add additional information – when and who. We can use this in some statistics or something else…