简体   繁体   中英

Creating custom FilterAttribute for method parameters

Is it possible to create a custom FilterAttribute for incoming parameters? Mostly attributes are used for methods and classes;

for example my idea is:

public HttpResponseMessage GetAll([CultureInfo]string culture)

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

CultureInfo is a class (CultureInfoAttribute) that inherits from ActionFilterAttribute where i can use 2 methods called, OnActionExecuted and OnActionExecuting. On top of my class i used the next attribute so i can use it on parameters:

[AttributeUsage(AttributeTargets.Parameter)]

when i build and i call GetAll("culture") the method OnActionExecuting in my custom filter never gets called....it does when i put the attribute above my method GetAll().

Anyone has expercience with it?

The reason i wanna do this is because i can put attributes on different parameters then instead of the whole method at once.

I think your question is answered here :

You should register your filter in GlobalFilters in Global.asax.cs like this:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
    filters.Add(new CultureInfoAttribute());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM