Skip to content

Instantly share code, notes, and snippets.

@Danthar
Created November 7, 2014 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Danthar/a41b42442d0d996e0903 to your computer and use it in GitHub Desktop.
Save Danthar/a41b42442d0d996e0903 to your computer and use it in GitHub Desktop.
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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment