Код: Выделить всё
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
public class CustomRegexValidator : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (!Regex.Match((string)value, "^[a-zA-Z0-9]+(?:-?[a-zA-Z0-9]+)+$").Success)
{
return new ValidationResult("Invalid Format");
}
return ValidationResult.Success;
}
}
Код: Выделить всё
{
"errors": {
"variableName": [
"Invalid Format"
]
},
"type": "https://tools.ietf.org/html/rfc9110#section-15.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-f8c2d18438cfa0cea6967f46a5bf8ba1-0f3447a15a1dbc-00"
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... ibute-in-c
Мобильная версия