asp.net-mvc 如何将变量传递给 ASP.NET MVC 应用程序中的自定义 ActionFilter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18209735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:04:58  来源:igfitidea点击:

How do I pass variables to a custom ActionFilter in ASP.NET MVC app

asp.net-mvcasp.net-mvc-3asp.net-mvc-4

提问by divyanshm

I have a controller in my MVC app for which I'm trying to log details using a custom ActionFilterAttribute, by using the onResultExecuted method.

我的 MVC 应用程序中有一个控制器,我尝试使用 onResultExecuted 方法使用自定义 ActionFilterAttribute 为其记录详细信息。

I read this tutorialto understand and write my own action filter. The question is how do I pass variables from the controller to the action filter?

我阅读本教程是为了理解和编写我自己的动作过滤器。问题是如何将变量从控制器传递到动作过滤器?

  1. I want to get the input variables with which a controller is called. Say, the username/user ID.
  2. If (in some situations) an exception is thrown by any controller method, I would want to log the error too.
  1. 我想获取调用控制器的输入变量。比如说,用户名/用户 ID。
  2. 如果(在某些情况下)任何控制器方法抛出异常,我也想记录错误。

The controller -

控制器——

[MyActionFilter]
public class myController : ApiController {
    public string Get(string x, int y) { .. }
    public string somemethod { .. }
}

The action filter -

动作过滤器 -

public class MyActionFilterAttribute : ActionFilterAttribute {
    public override void onActionExecuted(HttpActionExecutedContext actionExecutedContext) {
        // HOW DO I ACCESS THE VARIABLES OF THE CONTROLLER HERE
        // I NEED TO LOG THE EXCEPTIONS AND THE PARAMETERS PASSED TO THE CONTROLLER METHOD
    }
}

I hope I have explained the problem here. Apologies if I'm missing out some basic objects here, I'm totally new to this.

我希望我已经在这里解释了这个问题。抱歉,如果我在这里遗漏了一些基本对象,我对此完全陌生。

回答by Imad Alazani

Approach - 1

方法 - 1

Action Filter

动作过滤器

public class MyActionFilter : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        base.OnActionExecuted(filterContext);
    }
}

Action Method

动作方法

[MyActionFilter]
public ActionResult Index()
{
    ViewBag.ControllerVariable = "12";
    return View();
}

enter image description here

在此处输入图片说明

If you pay attention to the screenshot, you can see the ViewBaginformation

如果你注意截图,你可以看到ViewBag信息

Approach - 2

方法 - 2

Action Filter

动作过滤器

public class MyActionFilter : ActionFilterAttribute
{
    //Your Properties in Action Filter
    public string Property1 { get; set; }
    public string Property2 { get; set; }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);
    }
}

Action Method

动作方法

[MyActionFilter(Property1 = "Value1", Property2 = "Value2")]
public ActionResult Index()
{
    return View();
}