In .NET Core, you can create customdataannotations for custom validation on model properties by creating your own custom validation attributes. This allows you to define custom validation rules and apply them to specific properties in your models. Here's how you can create custom data annotations for custom validation:
Step 1: Create a Custom Validation Attribute Class:
You can create a custom validation attribute by inheriting from the ValidationAttribute class and overriding the
IsValid method. Here's an example of a custom validation attribute for checking if a string contains a specific keyword:
using System;
using System.ComponentModel.DataAnnotations;
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class MustContainAttribute : ValidationAttribute
{
private readonly string keyword;
public MustContainAttribute(string keyword)
{
this.keyword = keyword;
}
public override bool IsValid(object value)
{
if (value is string strValue && !string.IsNullOrEmpty(strValue))
{
return strValue.Contains(keyword);
}
return true; // By default, the property is considered valid if it's not a string or empty.
}
}
In this example, the MustContainAttribute checks if a string property contains a specified keyword.
Step 2: Apply the Custom Attribute to a Model Property:
Now, you can apply your custom attribute to a property in your model. For example:
public class MyModel
{
[MustContain("example")]
public string Description { get; set; }
}
In this case, the Description property is decorated with the
MustContain attribute, and it will be validated using the custom validation logic defined in the attribute.
Step 3: Perform Validation in Your Code:
In your application code, you can use the Validator.TryValidateObject method to perform the validation on the model:
var myModel = new MyModel { Description = "This is an example description." };
var validationContext = new ValidationContext(myModel);
var validationResults = new List<ValidationResult>();
if (!Validator.TryValidateObject(myModel, validationContext, validationResults, validateAllProperties: true))
{
foreach (var validationResult in validationResults)
{
Console.WriteLine(validationResult.ErrorMessage);
}
}
The TryValidateObject method checks the Description property using the custom validation attribute and reports any validation errors.
Custom data annotations are powerful tools for creating custom validation rules for your model properties in .NET Core. They allow you to encapsulate validation logic and apply it to specific properties as needed.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In .NET Core, you can create custom data annotations for custom validation on model properties by creating your own custom validation attributes. This allows you to define custom validation rules and apply them to specific properties in your models. Here's how you can create custom data annotations for custom validation:
Step 1: Create a Custom Validation Attribute Class:
You can create a custom validation attribute by inheriting from the ValidationAttribute class and overriding the IsValid method. Here's an example of a custom validation attribute for checking if a string contains a specific keyword:
In this example, the MustContainAttribute checks if a string property contains a specified keyword.
Step 2: Apply the Custom Attribute to a Model Property:
Now, you can apply your custom attribute to a property in your model. For example:
In this case, the Description property is decorated with the MustContain attribute, and it will be validated using the custom validation logic defined in the attribute.
Step 3: Perform Validation in Your Code:
In your application code, you can use the Validator.TryValidateObject method to perform the validation on the model:
The TryValidateObject method checks the Description property using the custom validation attribute and reports any validation errors.
Custom data annotations are powerful tools for creating custom validation rules for your model properties in .NET Core. They allow you to encapsulate validation logic and apply it to specific properties as needed.