asp.net-mvc – 将List参数传递到ASP.NET MVC3中的自定义操作过滤器

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 将List参数传递到ASP.NET MVC3中的自定义操作过滤器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我怎么能把List解析成自定义动作过滤器(比如输入参数)?
public class CustomFilter : ActionFilterAttribute
{

    public List<MyEnumType> InputParameter { get; set; }

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

[CustomFilter(InputParameter = new List<MyEnumType>() { MyEnumType.Type } )]
public SomeActionInController()
{
}

我得错了错误

'InputParameter' is not a valid named attribute argument because it is not a valid attribute parameter type

解决方法

操作过滤器参数是操作过滤器的属性
[CustomFilter(InputParameter=10)]
public SomeActionInController()
{
}

public class CustomFilter : ActionFilterAttribute
{
  public int InputParameter { get; set; }

  public override void OnActionExecuting(ActionExecutingContext filterContext)
  {
    // access this.InputParameter

    base.OnActionExecuting(filterContext);
  }
}

属性参数类型仅限于此处描述的类型 – http://msdn.microsoft.com/en-us/library/aa664615%28v=vs.71%29.aspx

您可以通过属性构造函数传递集合,如此处所述 – Can I initialize a C# attribute with an array or other variable number of arguments?

原文链接:https://www.f2er.com/aspnet/245137.html

猜你在找的asp.Net相关文章