Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create async validation attribute in WEB API 2

I heard that it is possible to create async validation attributes in ASP.NET WEB API 2, but I didn't find any examples. I would like to create a validation class like this one, but async way:

public class UserNameAvailable : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        var username = value as string;

        if(username != null)
        {
            return !Database.UserExists(username); // must be async
        }

        return true;
    }
}
like image 401
user1613797 Avatar asked Nov 11 '22 18:11

user1613797


1 Answers

By validation attribute, I am assuming you are referring to something like an ActionFitler attribute. In Web API 2, we do not yet have async version of methods in ActionFilter attributes yet. However, in the coming release we are supporting this feature.

Its still possible to create custom action filter attributes yourself which implement System.Web.Http.Filters.IActionFilter, but I am wondering if you would rather wait for the next release. if this cannot wait, let me know and I can try something out for you.

like image 173
Kiran Avatar answered Nov 15 '22 13:11

Kiran