为方法参数创建自定义FilterAttribute
问题描述:
是否可以为传入参数创建自定义FilterAttribute? 大部分属性用于方法和类;为方法参数创建自定义FilterAttribute
例如我的想法是:
public HttpResponseMessage GetAll([CultureInfo]string culture)
{ if(valid) { // code here } }
CultureInfo的是一类(CultureInfoAttribute),其从ActionFilterAttribute继承其中i可使用2个方法调用,OnActionExecuted和OnActionExecuting。 在班里的第一名,我用的下一个属性,所以我可以在参数使用它:在我的自定义过滤器
[AttributeUsage(AttributeTargets.Parameter)]
当我建立我叫GETALL(“文化”)的方法OnActionExecuting不会被调用....当我将该属性放在我的方法GetAll()上时,它会执行此操作。
任何人都有它的经验吗?
我想这样做的原因是因为我可以把属性放在不同的参数上,而不是一次完成整个方法。
答
我认为你的问题的回答here:
你应该在Global.asax.cs
登记GlobalFilters
你的过滤器是这样的:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new CultureInfoAttribute());
}