2 回答
TA贡献1848条经验 获得超10个赞
一种方法是将路由参数的名称传递给属性,并检查属性本身中的路由数据。例如:
[SecureMethod(PermissionRequired = AccessPermissionEnum.DeleteUsers, UserIdsKey = "userIds")]
然后是这样的:
public AccessPermissionEnum PermissionRequired { get; set; }
public string UserIdsKey { get; set; }
public override void OnActionExecuting(ActionExecutingContext context)
{
// Depending on how you pass your userIds to the action, you
// may need to use something else from context to get the data,
// but it's all available in there.
var userIds = context.ActionArguments[UserIdsKey];
// Do something with the ids.
base.OnActionExecuting(context);
}
虽然这行得通,并且在某些地方确实行之有效,但您应该知道过滤器现在对传入的参数有深入的了解。在大多数情况下,这通常无关紧要,但这是您需要的一种方法不应该到处使用,因为如果使用不当,最终会增加很多耦合。这是一个不时使用的有用工具。
TA贡献1852条经验 获得超1个赞
不,这是不可能的。
属性参数仅限于以下类型的常量值:
简单类型(bool、byte、char、short、int、long、float 和 double)
细绳
系统类型
枚举
对象(对象类型的属性参数的参数必须是上述类型之一的常量值。)
任何上述类型的一维数组
您不能嵌套属性,也不能将非常量值传递给属性参数。即使您可以将属性参数声明为类型对象,您传入的实际参数仍然必须是常量字符串、bool 等(将其声明为对象仅意味着您可以在每次声明属性时使用这些类型中的任何一种) .
- 2 回答
- 0 关注
- 223 浏览
添加回答
举报
