MVC操作过滤器使用传递给为的ActionResult参数? [英] MVC Action Filters using parameters passed to the for ActionResult?

查看:1400
本文介绍了MVC操作过滤器使用传递给为的ActionResult参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义的<一个href=\"http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs\">Action过滤器没有问题。

I created a custom Action Filter with no problem.

不过,我想修改的行为过滤器使用一些实际传递给我的方法的参数。

But I would like to modify the Action Filter to use some of the parameters actually passed to my method.

所以,如果我有以下方式:

So if I have the following method:

[HttpPost]
[MyAttribute]
public ActionResult ViewUserDetails(Guid userId)
{
     // Do something
}

我怎样才能从内部 MyAttribute 可以访问用户id?有没有一种方法,我可以直接将它?

How can I get access to userId from within MyAttribute? Is there a way I can directly pass it in?

推荐答案

您可以尝试 OnActionExecuting 覆盖,在那里你可以访问动作参数。

You can try OnActionExecuting override, where you do have access to action parameters.

public class MyAttribute: ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {    
        if (filterContext.ActionParameters.ContainsKey("userId"))
        {
            var userId = filterContext.ActionParameters["userId"] as Guid;
            if (userId != null)
            {
                // Really?! Great!            
            }
        }
    }
} 

这篇关于MVC操作过滤器使用传递给为的ActionResult参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆