获取请求参数的值

获取请求参数的值

1.在OnActionExecuting里 获取请求参数的值 比较简单

/// <summary>
/// 获取首参数的值
/// </summary>
/// <param name="filterContext">The filter context.</param>
/// <returns></returns>
private object GetFirstParamObject(ActionExecutingContext filterContext)
{
var paramNames = filterContext.ActionDescriptor.GetParameters();
if (paramNames.Length > )
{
var parameterInfo = filterContext.ActionParameters[paramNames[].ParameterName]; return parameterInfo;
} return null;
}

2.在OnActionExecuted 获取请求参数的值(包含类类型)

        /// <summary>
/// OnActionExecuted 获取请求参数的值
/// </summary>
/// <param name="filterContext">filterContext</param>
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
try
{
//请求类各个字段的值
Dictionary<string, string> parmsObj = new Dictionary<string, string>(); foreach (var item in filterContext.ActionDescriptor.GetParameters())
{
var itemType = item.ParameterType;
if (itemType.IsClass && itemType.Name != "String")
{
PropertyInfo[] infos = itemType.GetProperties();
foreach (PropertyInfo info in infos)
{
if (info.CanRead)
{
var propertyValue = filterContext.Controller.ValueProvider.GetValue(info.Name);// 暂不支持多层嵌套 后期优化?
if (!parmsObj.ContainsKey(info.Name))
{
parmsObj.Add(info.Name, null == propertyValue ? "" : propertyValue.AttemptedValue);
}
}
}
}
else
{
var parameterValue = filterContext.Controller.ValueProvider.GetValue(item.ParameterName);
if (!parmsObj.ContainsKey(item.ParameterName))
{
parmsObj.Add(item.ParameterName, null == parameterValue ? "" : parameterValue.AttemptedValue);
}
}
}
}
05-11 11:28