1

是否可以为传入参数创建自定义 FilterAttribute?大多数属性用于方法和类;

例如我的想法是:

public HttpResponseMessage GetAll([CultureInfo]string culture)

{ if(valid) { // code here } }

CultureInfo 是一个继承自 ActionFilterAttribute 的类 (CultureInfoAttribute),我可以在其中使用 2 个称为 OnActionExecuted 和 OnActionExecuting 的方法。在我的课堂上,我使用了下一个属性,所以我可以在参数上使用它:

[AttributeUsage(AttributeTargets.Parameter)]

当我构建并调用 GetAll("culture") 时,我的自定义过滤器中的 OnActionExecuting 方法永远不会被调用....当我将属性放在我的方法 GetAll() 之上时,它确实如此。

有人有经验吗?

我想这样做的原因是因为我可以将属性放在不同的参数上,而不是一次放在整个方法上。

4

1 回答 1

0

我想你的问题在这里得到了回答:

您应该像这样注册您的过滤GlobalFiltersGlobal.asax.cs

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
    filters.Add(new CultureInfoAttribute());
}
于 2013-02-05T11:11:03.950 回答