简体   繁体   English

为MVC选择最佳的客户端/服务器验证框架

[英]Choose the best Client/Server Validation Framework for MVC

Im trying to choose the best server/client validation framework to use with my MVC application. 我试图选择最好的服务器/客户端验证框架来与我的MVC应用程序一起使用。 I would like to know what is the best validation framework to use with MVC to do server/client validation. 我想知道与MVC一起用于服务器/客户端验证的最佳验证框架是什么。

My options are: 我的选择是:

  • Castle validator 城堡验证器
  • Microsoft Data Annotations Microsoft数据注释
  • nHibernate Validator nHibernate验证程序

With the upcoming MVC 2, it looks like MS is leaning towards the System.ComponentModel.DataAnnotations library. 在即将发布的MVC 2中,MS似乎倾向于使用System.ComponentModel.DataAnnotations库。 It's pretty nice - does a lot of code generation for you. 非常好-为您完成了很多代码生成。

I've been using xVal with great success. 我一直在使用xVal取得巨大成功。

The best thing is you can fully automate validation: 最好的事情是您可以完全自动化验证:

  • put DataAnnotations (or special rules) to your business layer POCO classes (if you have them) or entity classes' metadata buddy classes if you use entities up to controller layer DataAnnotations (或特殊规则)放入您的业务层POCO类(如果有)或实体类的元数据伙伴类(如果您使用的是直到控制器层的实体)
  • write an ActionFilter that will automatically validate parameters to your controller actions - it's best if all your POCOs (or entities) implement a certain interface that defines Validate() method, filter calls this method and fills ModelState.Errors when validation fails. 编写一个ActionFilter来自动验证控制器操作的参数-最好是所有POCO(或实体)都实现一个定义Validate()方法的特定接口,filter调用此方法并在验证失败时填充ModelState.Errors
  • Add if (ModelState.IsValid) { ... } in your controller action that needs to work differently when model isn't valid 在模型无效的控制器操作中添加if (ModelState.IsValid) { ... }
  • Put <%= Html.ValidationMessage(...) %> in your views that will display validation errors 在视图中放入<%= Html.ValidationMessage(...)%>,将显示验证错误
  • Add xVal's client validation to your views if desired 如果需要,将xVal的客户端验证添加到您的视图中

This way you've set up automatic object validation. 这样,您可以设置自动对象验证。 All your controller actions will do is check Model validity. 您的控制器所有动作将是检查模型有效性。

Asp.net MVC 2 will have something very similar to xVal already built in the framework so it's up to you which version are you using. Asp.net MVC 2具有与框架中已内置的xVal非常相似的功能,因此由您决定使用哪个版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM