Welcome to RewaTechForge.com. Feel free to have a look around and leave a comment.

Meta
data annotation

Introduction

In our earlier entries in this series on securing web applications, we introduced data annotations as rules defined within the model class. We demonstrated their application using simple annotations on the Customer class. In this post, we will define data annotations and explore common types that you can effectively utilize in your applications.

Data annotations are a set of attributes in ASP.NET Core MVC that you can apply to model properties to specify validation rules, formatting options, display options, and more. These attributes help you define the behavior of your models and enforce data integrity in your application.

Common Data Annotations

Here’s a comprehensive list of commonly used data annotation attributes in ASP.NET Core MVC, along with descriptions of their usage:

AttributesDescriptionUsage
[Required]Specifies that a property is required and must have a non-null value.[Required]
public string Name { get; set; }
[StringLength]Specifies the minimum and maximum length constraints for strings.
Parameters:
MinimumLength: Specifies the minimum length of the string.
MaximumLength: Specifies the maximum length of the string.
[StringLength(50, MinimumLength = 2)]
public string Description { get; set; }
[Range]Specifies numeric range constraints for integer and floating-point properties.
Parameters:
Minimum: Specifies the minimum allowed value.
Maximum: Specifies the maximum allowed value.
[Range(1, 100)]
public int Age { get; set; }
[RegularExpression]Specifies a regular expression pattern that the property value must match.
Parameters:
Pattern: Specifies the regular expression pattern.
[RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$")]
public string LastName { get; set; }
[EmailAddress]Specifies that the property value must be a valid email address.[EmailAddress]
public string Email { get; set; }
[DataType]Specifies the data type of the property.
Parameters:
DataType: Specifies the data type (e.g., DataType.Date, DataType.Time, DataType.PhoneNumber).
[DataType(DataType.Date)]
public DateTime BirthDate { get; set; }
[Display]Specifies display-related metadata for the property.
Parameters:
Name: Specifies the display name for the property.
[Display(Name = "First Name")]
public string FirstName { get; set; }
[DisplayFormat]Specifies formatting options for the property value.
Parameters:
DataFormatString: Specifies the format string for formatting the property value.
[DisplayFormat(DataFormatString = "{0:C}")]
public decimal Price { get; set; }
[Url]Specifies that the property value must be a valid URL.[Url]
public string WebsiteUrl { get; set; }
[MinLength]Specifies the minimum length of a string or array.
Parameters:
Length: Specifies the minimum length.
[MinLength(2)]
public string UserName { get; set; }
[MaxLength]: Specifies the allowed file extensions for file upload properties.
Parameters:
Extensions: Specifies the allowed file extensions.
[MaxLength(100)]
public string Address { get; set; }
[FileExtensions]Specifies the allowed file extensions for file upload properties.
Parameters:
Extensions: Specifies the allowed file extensions.
[FileExtensions(Extensions = ".jpg,.jpeg,.png")]
public IFormFile Photo { get; set; }
[DataType(DataType.Password)]Specifies that the property represents a password field.[DataType(DataType.Password)]
public string Password { get; set; }
[Compare]Specifies that the property value must match the value of another property.
Parameters:

OtherProperty: Specifies the name of the other property to compare against.
[Compare("Password")]
public string ConfirmPassword { get; set; }
List of common data annotation attributes

Conclusion

In conclusion, our exploration of ASP.NET validation continues as we focus on a range of common data attributes. There may be instances where you need to go beyond the standard attributes provided out of the box. In our upcoming post, we will delve into how to extend these predefined annotation attributes to better suit your specific requirements.

Meanwhile, we greatly value your feedback and are eager to hear your thoughts on this discussion. Please feel free to comment below with any feedback or suggest topics you’d like us to explore in future posts. Your insights and experiences are vital in enriching our content and making this series a comprehensive resource for developers everywhere.

By Admin

3 thoughts on “Data Annotations”

Leave a Reply

Your email address will not be published. Required fields are marked *