LSTANCZYK
3/21/2017 - 4:44 PM

Example of mapping action parameters in an actionattribute for MVC

Example of mapping action parameters in an actionattribute for MVC

public class LogAttribute : ActionFilterAttribute {
        private IDictionary<string, object> parameters;
        private string description;

        public LogAttribute(string description) {
            this.description = description;
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext) {
            parameters = filterContext.ActionParameters;
            base.OnActionExecuting(filterContext);
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext) {
            
            //simplest thing that could possibly work
            foreach (var kvp in parameters) {
                description = description.Replace("{"+kvp.Key+"}", kvp.Value.ToString())
            }

            //then do with the string whatever you want
        }
    }