我如何使用ValidationAttribute验证HttpGet方法中的字符串参数:
public class LocaleAttribute : ValidationAttribute
{
public LocaleAttribute() : base(UserInteractionErrors.InvalidLocale) { }
public override bool IsValid(object value)
{
if (value is string && CultureInfo.GetCultures(CultureTypes.AllCultures).Any(c => string.Equals(c.Name, (string)value, StringComparison.OrdinalIgnoreCase)))
return true;
else
return false;
}
}
[ApiController]
public class ConsentController : Controller
{
[HttpGet]
[Route("api/get/{locale}")]
public async Task<IActionResult> Get([Locale] string locale)
{
something
}
}
未调用IsValid方法。 有什么想法吗?