通过列表<&枚举GT;参数到ASP.NET MVC3自定义操作过滤器 [英] Passing List<Enum> parameters into custom action filter in ASP.NET MVC3

查看:139
本文介绍了通过列表<&枚举GT;参数到ASP.NET MVC3自定义操作过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能解析名单了我的自定义操作过滤器(如输入参数)?

 公共类CustomFilter:ActionFilterAttribute
{    公开名单< MyEnumType> InputParameter {搞定;组; }    公共覆盖无效OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);
    }
}[CustomFilter(InputParameter =新的List< MyEnumType>(){} MyEnumType.Type)
公共SomeActionInController()
{
}

我errror错误

 'InputParameter'不是一个有效的命名属性的参数,因为它不是一个有效的属性参数类型


解决方案

动作滤波器参数的动作滤镜的属性:

  [CustomFilter(InputParameter = 10)]
公共SomeActionInController()
{
}公共类CustomFilter:ActionFilterAttribute
{
  公众诠释InputParameter {搞定;组; }  公共覆盖无效OnActionExecuting(ActionExecutingContext filterContext)
  {
    //访问this.InputParameter    base.OnActionExecuting(filterContext);
  }
}

属性参数类型仅限于这里所描述的类型 - <一href=\"http://msdn.microsoft.com/en-us/library/aa664615%28v=vs.71%29.aspx\">http://msdn.microsoft.com/en-us/library/aa664615%28v=vs.71%29.aspx

您可以按此处所述通过属性构造通过集合 - <一个href=\"http://stackoverflow.com/questions/270187/can-i-initialize-a-c-attribute-with-an-array-or-other-variable-number-of-argumen\">Can我初始化数组或参数其他变量数量C#属性

how I can parse List into me custom action filter (like input parameters) ?

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()
{
}

I got errror Error

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

解决方案

Action filter parameters are properties of the action filter:

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

Attribute parameter types are limited to the types described here - http://msdn.microsoft.com/en-us/library/aa664615%28v=vs.71%29.aspx

You can pass a collection via the attributes constructor as described here - Can I initialize a C# attribute with an array or other variable number of arguments

这篇关于通过列表&LT;&枚举GT;参数到ASP.NET MVC3自定义操作过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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