How to validate string parameter with ValidationAttribute
How to validate string parameter with ValidationAttribute
how can i validate a string parameter in a HttpGet method using ValidationAttribute:
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
}
}
Any ideeas?
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What doesn't work with the posted code?
– Camilo Terevinto
4 mins ago