如何在OnActionExecuted中获取ActionParameters

时间:2013-09-19 23:25:29

标签: asp.net-mvc-4 action-filter

我正在构建用户调用的操作历史记录。为此,我需要原始查询字符串中的所有参数。这需要在OnActionExecuted(动作之后)完成,因为一些描述信息存储在filterAction.Result.Model.MyDescription中。

在ActionExecutedContext上,我无权访问ActionParameters,而RoutesValues只包含action,controller,id和culture。它不包含查询字符串中包含的任何其他参数。

如何将OnActionExecuting的ActionParameters保存到OnActionExecuted?

控制器:

[ReportHistoryLink]
public ActionResult Index(int id, DateTime at, string by)
{
    var model = new ReportModel();
    model.ReportDescription = "Some logic get the ReportDescription";
    return View("Index", model);
}

ActionFilter:

public class ReportHistoryLinkAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        var model = (IModelWithReportDescription)((ViewResult)filterContext.Result).Model;
        var description = model.ReportDescription;

        var boxedId = filterContext.RouteData.Values["id"];
        var id = Convert.ToInt32(boxedId);
        if (id != 0 && !string.IsNullOrWhiteSpace(targetDescription))
        {
            var link = new HistoryLink
            {
                Id = id,
                Description = description,
                Route = filterContext.RouteData.Values  //This doesn't contains the "at and by parameters"
            };
        }
    }
}

1 个答案:

答案 0 :(得分:3)

您可以使用

filterContext.HttpContext.Request.QueryString["id"]