December 2019
Intermediate to advanced
510 pages
11h 33m
English
ASP.NET Core provides a way for us to create custom validations for our requests by extending ValidationAttribute, which means we can create custom validators for our types. Let's create a more appropriate validation for our Currency attribute:
using System.Collections.Generic;using System.ComponentModel.DataAnnotations;using System.Linq;namespace SampleAPI.Requests{ public class CurrencyAttribute : ValidationAttribute { private readonly IList<string> _acceptedCurrencyCodes = new List<string>{ "EUR", "USD", "GBP" }; protected override ValidationResult IsValid(object value, ValidationContext validationContext) { return _acceptedCurrencyCodes.Any(c => c == value.ToString()) ? ValidationResult.Success : new
Read now
Unlock full access