I want to call a specific method when any actions are called to do some work. I was wondering if this was possible? I do have a basecontroller for all the controllers but currently there is nothing in there.
ie If I go to the index page a call is made to the controller to the index action method when that method is called I want to do some work then continue with the action. I am trying to avoid calling this method all over the place in every single action and see if there is a universal way of calling it every where.
Yes, there is something exactly for this. You're looking for filters , more specifically the OnActionExecuting
one.
If this method is overridden in a derived Controller class, it will be called for every action method in the class. For more flexibility, derive a class from ActionFilterAttribute and override this method in the derived ActionFilterAttribute class.
Sample:
protected override void OnActionExecuting(ActionExecutingContext ctx) {
base.OnActionExecuting(ctx);
ctx.HttpContext.Trace.Write("Log: OnActionExecuting",
"Calling " +
ctx.ActionDescriptor.ActionName);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.