假设我有一个这样的控制器方法:
[Audit]
public JsonNetResult List(int start, int limit, string sort, string dir, string searchValue, SecurityInputModel securityData)
{
...
}
和一个定义如下的属性:
[AttributeUsage(AttributeTargets.Method)]
public class AuditAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
// auditing code here
base.OnActionExecuted(filterContext);
}
}
我可以从OnActionExecuted()里面获取start / limit / sort / etc的值吗?
答案 0 :(得分:11)
您可以使用ActionExecutingContext.ActionParameters属性在OnActionExecuting
中获取参数值。
例如,以下test属性将参数名称和值写入响应(ItemModel
类覆盖ToString
以仅输出其2个属性):
public class CustomActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var response = filterContext.HttpContext.Response;
response.Write(filterContext.ActionDescriptor.ActionName);
response.Write("<br/>");
foreach (var parameter in filterContext.ActionParameters)
{
response.Write(string.Format("{0}: {1}", parameter.Key, parameter.Value));
}
}
}
[CustomActionFilter]
[HttpPost]
public ViewResult Test(ItemModel model)
{
return View(model);
}