Created
November 7, 2014 13:15
-
-
Save Danthar/a41b42442d0d996e0903 to your computer and use it in GitHub Desktop.
Example of mapping action parameters in an actionattribute for MVC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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