Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Fluent Validation verified by ASP.NET MVC5

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail the example analysis of Fluent Validation for ASP.NET MVC5 verification. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Fluent Validation is an open source .NET class library that uses the Fluent interface and lambda expressions to validate entities. Fluent Validation is used specifically for entity validation. Its advantage is to separate the validation logic from the business logic of your code. This is what AOP thinks. Is crosscutting concerns. You only need to focus on one module. This ensures the purity of the code.

Fluent Validation open source address: https://github.com/JeremySkinner/fluentvalidation

Example:

Aspect-oriented program is a new software development paradigm that enables modular implementation of cross-cutting concerns,and poses difficulties for slicing of aspect-oriented programs.

As a new software development paradigm, aspect-oriented programming can realize the modularization of crosscutting concerns, and its unique language elements and functions increase the difficulty of slicing.

All right, there's too much nonsense. Let's get right to the point.

First, let's create a new blank MVC project: create a new class Customer under the Model folder:

Using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace Server_Side_Validation_IN_MVC.Models {public class Customer {public string Name {get; set;} public string Email {get; set;}

Then create a new folder Validator and add a class CustomerValidator to it

Since you are going to use Fluent Validation, it is time to reference its class library.

In the CustomerValidator class, inherit the AbstractValidator abstract class, (PS: this is similar to the Fluent API in EF, and the EntityTypeConfiguration class is inherited in EF)

Using FluentValidation;using Server_Side_Validation_IN_MVC.Models;using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace Server_Side_Validation_IN_MVC.Validator {public class CustomerValidator:AbstractValidator {public CustomerValidator () {RuleFor (s = > s.Name) .NotEmpty () .WithMessage ("name cannot be empty"); RuleFor (s = > s.Email) .NotEmpty () .WithMessage ("email cannot be empty") RuleFor (s = > s.Email). EmailAddress (). WithMessage ("illegal email format");}

Code in the controller:

Using FluentValidation.Results;using Server_Side_Validation_IN_MVC.Models;using Server_Side_Validation_IN_MVC.Validator;using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace Server_Side_Validation_IN_MVC.Controllers {public class CustomerController: Controller {/ / GET: Customer public ActionResult Index () {return View ();} [HttpPost] public ActionResult Index (Customer model) {CustomerValidator validator = new CustomerValidator (); ValidationResult result = validator.Validate (model) If (result.IsValid) {ViewBag.Name = model.Name; ViewBag.Email = model.Email;} else {foreach (var item in result.Errors) {ModelState.AddModelError (item.PropertyName, item.ErrorMessage);}} return View (model);}

Modify the default route:

Public static void RegisterRoutes (RouteCollection routes) {routes.IgnoreRoute ("{resource} .axd / {* pathInfo}"); routes.MapRoute (name: "Default", url: "{controller} / {action} / {id}", defaults: new {controller = "Customer", action = "Index", id = UrlParameter.Optional});}

Don't enter anything, just click Create:

Enter Name instead of Email

Input Name,Email input illegal data

Enter legal data:

This completes the Fluent Validation verification. As you can see, this kind of verification is much cleaner and concise, and the configuration information is all in one class, which is easy to maintain and expand. Instead of mixing validation information with entities like data annotations.

This is the end of this article on "sample Analysis of Fluent Validation verified by ASP.NET MVC5". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report