3

I used to believe that the following method would be invoked after all controller methods are done at the end:

protected override void EndExecute(IAsyncResult asyncResult) 

Now all override methods are called and then, controller methods are invoked. Do you know what override method is invoked after all methods?

5
  • Perhaps an action filter can do whatever you want to do ? Commented Jul 19, 2018 at 5:27
  • Could you provide me an example? Commented Jul 19, 2018 at 5:31
  • What are you trying to do ? Can you be more specific ? Commented Jul 19, 2018 at 5:34
  • I want to add something to ViewBag at the end of all controller methods. The item to be added may vary for each method. Commented Jul 19, 2018 at 5:40
  • vary like what ? Where do you get that data you want to add to viewbag ? Commented Jul 19, 2018 at 5:41

1 Answer 1

5

You can use action filter it will be called every time any method execute. You can filter it by matching method name.

 public class LogActionFilter : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { Log("OnActionExecuting", filterContext.RouteData); } public override void OnActionExecuted(ActionExecutedContext filterContext) { Log("OnActionExecuted", filterContext.RouteData); } public override void OnResultExecuting(ResultExecutingContext filterContext) { Log("OnResultExecuting", filterContext.RouteData); } public override void OnResultExecuted(ResultExecutedContext filterContext) { Log("OnResultExecuted", filterContext.RouteData); } private void Log(string methodName, RouteData routeData) { var controllerName = routeData.Values["controller"]; var actionName = routeData.Values["action"]; var message = String.Format("{0} controller:{1} action:{2}", methodName, controllerName, actionName); Debug.WriteLine(message, "Action Filter Log"); } } 

For more details please visit : https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing/understanding-action-filters-cs

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.