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 > 0) { var parameterInfo = filterContext.ActionParameters[paramNames[0].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); } } } }