Created
December 12, 2016 11:38
-
-
Save code-atom/a88f4b4e6c961aa3fc5fc4c7f8752d8e to your computer and use it in GitHub Desktop.
Bind value of action parameter with multiple name specify in list
This file contains hidden or 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
[System.AttributeUsage(System.AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)] | |
public class MultipleNameBindAttribute : Attribute | |
{ | |
public MultipleNameBindAttribute(string parameters) | |
{ | |
if (!String.IsNullOrEmpty(parameters)) | |
{ | |
AvailableName = parameters.Split(','); | |
} | |
} | |
public string[] AvailableName { get; private set; } | |
} | |
public class MultipleParameterNameActionFilter : ActionFilterAttribute | |
{ | |
public override void OnActionExecuting(ActionExecutingContext filterContext) | |
{ | |
var defaults = GetDefaults(filterContext); | |
var actionParameters = filterContext.ActionParameters; | |
foreach (var value in defaults) | |
if (actionParameters[value.Key] == null) | |
actionParameters[value.Key] = value.Value; | |
} | |
internal static IDictionary<string, object> GetDefaults(ActionExecutingContext filterContext) | |
{ | |
IDictionary<string, object> defaults = null; | |
if (defaults == null) | |
{ | |
defaults = new Dictionary<string, object>(filterContext.ActionParameters.Count); | |
foreach (var parameter in filterContext.ActionDescriptor.GetParameters()) | |
{ | |
if (parameter.IsDefined(typeof(MultipleNameBindAttribute), false)) | |
{ | |
MultipleNameBindAttribute attr = parameter.GetCustomAttributes(typeof(MultipleNameBindAttribute), false)[0] as MultipleNameBindAttribute; | |
string parameterName = parameter.ParameterName; | |
object parameterValue = filterContext.ActionParameters[parameterName]; | |
foreach (var name in attr.AvailableName) | |
{ | |
var value = filterContext.HttpContext.Request[name]; | |
if (string.IsNullOrEmpty(value)) continue; | |
parameterValue = value; | |
} | |
try | |
{ | |
defaults.Add(parameterName, Convert.ChangeType(parameterValue, parameter.ParameterType)); | |
} | |
catch (Exception exc) | |
{ | |
throw new InvalidOperationException(String.Format( | |
CultureInfo.CurrentUICulture, | |
"The value of the DefaultAttribute could not be converted to the parameter '{0}'", | |
parameterName), exc); | |
} | |
} | |
} | |
} | |
return defaults; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment