How to validate string parameter with ValidationAttribute

The name of the picture


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?





What doesn't work with the posted code?
– Camilo Terevinto
4 mins ago









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.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived