【问题标题】:How do I get ActionParameters in OnActionExecuted如何在 OnActionExecuted 中获取 ActionParameters
【发布时间】:2013-09-19 23:25:28
【问题描述】:

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

在 ActionExecutedContext 上,我无权访问 ActionParameters,RoutesValues 仅包含操作、控制器、id 和文化。它不包含查询字符串中包含的任何其他参数。

如何保留从 OnActionExecuting 到 OnActionExecuted 的 ActionParameters?

控制器:

[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);
}

动作过滤器:

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"
            };
        }
    }
}

【问题讨论】:

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


    【解决方案1】:

    你可以使用

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

    【讨论】:

    • 还要检查this。希望对某人有所帮助。
    • 请记住,这可以获得在当前操作方法中甚至不作为参数存在的值!为了得到你所做的那些filterContext.ActionParameters["parameterName"]
    • filterContext 不包含 ActionParameters
    • @stepone 是对的。 QueryString 包含字符串。动作参数被解析为类型。
    • 我不知道为什么这对我不起作用。查询字符串一直为空,我正在发送参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 2012-03-26
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    相关资源
    最近更新 更多