'Call Action Filter / Attribute on a single parameter in method
How would one go about calling an action filter (or an attribute) on a single parameter in an action in a controller?
I have an action that accepts a string parameter that needs to be transformed before the action can continue using it (since it could arrive from the front-end both transformed OR original). I want to implement an attribute that intercepts the action, transforms the parameter, and then continues the action (similar to how the [Bind] attribute is called on specific parameters in an action).
The example action filter gets called if I call it on the whole action but I want it to be called only on specific parameters.
Example action:
public ActionResult CalculateFieldValue(string documentId, [FieldEscape]string fieldId)
{
// Operations requiring the fieldId to be cleaned first...
}
Example action filter:
[AttributeUsage(AttributeTargets.Parameter)]
public class FieldEscapeAttribute: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// Replace the last occurence of "___" with ":" in the parameter value and then continue
base.OnActionExecuting(filterContext);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
