asp.net-mvc ASP.NET MVC 将对象从自定义操作过滤器传递到操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1809042/
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-07 23:57:49  来源:igfitidea点击:

ASP.NET MVC Pass object from Custom Action Filter to Action

asp.net-mvcfiltercustom-action

提问by reach4thelasers

If I create an object in a Custom Action Filter in ASP.NET MVC in

如果我在 ASP.NET MVC 中的自定义操作过滤器中创建一个对象

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    DetachedCriteria criteria = DetachedCriteria.For<Person>();
    criteria.Add("stuff");

    // Now I need to access 'criteria' from the Action.....

}

is there any way I can access the object from the Action that is currently executing.

有什么方法可以从当前正在执行的操作中访问对象。

采纳答案by Chad Moran

I would recommend putting it in the Route data.

我建议将它放在 Route 数据中。

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.RouteData.Values.Add("test", "TESTING");
        base.OnActionExecuting(filterContext);
    }

    public ActionResult Index()
    {
        ViewData["Message"] = RouteData.Values["test"];

        return View();
    }

回答by niaher

The better approachis described by Phil Haack.

更好的办法是通过菲尔哈克描述。

Basically this is what you do:

基本上这就是你要做的:

public class AddActionParameterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);

        // Create integer parameter.
        filterContext.ActionParameters["number"] = 123;

        // Create object parameter.
        filterContext.ActionParameters["person"] = new Person("John", "Smith");
    }
}

The only gotcha is that if you are creating object parameters, then your class (in this case Person) must have a default constructor, otherwise you will get an exception.

唯一的问题是,如果您正在创建对象参数,那么您的类(在本例中为 Person)必须有一个默认构造函数,否则您将收到异常。

Here's how you'd use the above filter:

以下是您如何使用上述过滤器:

[AddActionParameter]
public ActionResult Index(int number, Person person)
{
    // Now you can use number and person variables.
    return View();
}

回答by Darin Dimitrov

You could use the HttpContext:

你可以使用HttpContext

filterContext.HttpContext.Items["criteria"] = criteria;

And you can read it in the action:

你可以在行动中阅读它:

[YourActionFilter]
public ActionResult SomeAction() 
{
    var criteria = HttpContext.Items["criteria"] as DetachedCriteria;
}

回答by Mathias F

Set item in ViewData or of a viewmodel if you pass it as a parameter into your action. Here I set the property of a ViewModel

如果您将项目作为参数传递给您的操作,请在 ViewData 或视图模型中设置项目。这里我设置了一个 ViewModel 的属性

public override void OnActionExecuting(ActionExecutingContext filterContext)
 {
     ViewModelBase viewModel = null;
     foreach (object parameter in filterContext.ActionParameters.Values)
     {
         if (parameter is ViewModelBase)
         {
             viewModel = (ViewModelBase)parameter;
             break;
         }
     }
     if(viewModel !=null)
     {
         viewModel.SomeProperty = "SomeValue";
     }
 }


    public ActionResult About(ViewModelBase model)
    {
      string someProperty= model.SomeProperty;
}

Here is the untyped version I think you prefer:

这是我认为您更喜欢的无类型版本:

   public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.Controller.ViewData.Add("TestValue", "test");

    }

       [FilterWhichSetsValue]
        public ActionResult About()
        {
            string test = (string)ViewData["TestValue"];
            return View();
        }