ASP.NET MVC 2在您的视图上下文之外的模型验证

时间:2022-03-31 16:36:59

I was reading this blog post on ASP.NET MVC 2's new model validation and found it to be pretty cool. I see the value in keeping things DRY by adding a couple attributes to a class property and then automagically getting client and server side validation (as long as your controller checks for valid models).

我正在阅读关于ASP.NET MVC 2的新模型验证的博客文章,并发现它非常酷。通过向类属性添加一些属性然后自动获得客户端和服务器端验证(只要您的控制器检查有效模型),我看到了保持DRY的价值。

That said, imagine an ASP.NET MVC application in a greater context. Perhaps I have an ASP.NET MVC application with this validation and all, but then I want to expose things to new clients, like an iPhone or Android application (not just a mobile browser). I would have to write some web-services or something that use the same underlying repositories/services that my ASP.NET MVC app uses. Could I re-use this validation stuff?

也就是说,想象一下在更大的上下文中的ASP.NET MVC应用程序。也许我有一个带有此验证的ASP.NET MVC应用程序,但后来我想向新客户端公开,比如iPhone或Android应用程序(不仅仅是移动浏览器)。我将不得不编写一些Web服务或使用我的ASP.NET MVC应用程序使用的相同底层存储库/服务的东西。我可以重复使用这个验证的东西吗?

Wouldn't I have to re-write the client-side validation? I don't see a way around this, since I'll be shipping off serialized objects to be de-serialized into non-C# classes from Objective-C or whatever.

我不是必须重新编写客户端验证吗?我没有看到解决这个问题的方法,因为我将从序列化对象发送出去,从Objective-C或其他任何东西反序列化为非C#类。

Wouldn't I also have to re-write the server-side validation? Is there some way for me to call into the ModelState on an object outside the context of an ASP.NET MVC controller (e.g., as part of the web-services I create to talk to mobile clients).

我不也必须重新编写服务器端验证吗?有没有办法让我在ASP.NET MVC控制器上下文之外的对象上调用ModelState(例如,作为我创建的与移动客户端通信的Web服务的一部分)。

1 个解决方案

#1


1  

Assuming you are talking about Data Annotations, they are not part of MVC actually. So you can use them elsewhere as you wish.

假设您正在讨论数据注释,它们实际上不是MVC的一部分。所以你可以随意在别处使用它们。

There are other work extending them - annotationscontrib is one of them and I guess you can look at how, for example ModelBinder is implemented and write your own service using that.

还有其他工作可以扩展它们 - annotationscontrib就是其中之一,我想你可以看看如何实现ModelBinder,并使用它来编写自己的服务。

xVal Project is possibly doing along the same lines as what you are after - converting the validation rules into Javascript. Unfortunately I am not sure how much effort you need to adapt xVal for your project or rewrite it.

xVal Project可能与您所使用的相同 - 将验证规则转换为Javascript。不幸的是,我不确定您需要多少努力才能使xVal适应您的项目或重写它。

As for running the validations on the annotated classes one can use the Validator (thanks womp)

至于在带注释的类上运行验证,可以使用Validator(感谢womp)

#1


1  

Assuming you are talking about Data Annotations, they are not part of MVC actually. So you can use them elsewhere as you wish.

假设您正在讨论数据注释,它们实际上不是MVC的一部分。所以你可以随意在别处使用它们。

There are other work extending them - annotationscontrib is one of them and I guess you can look at how, for example ModelBinder is implemented and write your own service using that.

还有其他工作可以扩展它们 - annotationscontrib就是其中之一,我想你可以看看如何实现ModelBinder,并使用它来编写自己的服务。

xVal Project is possibly doing along the same lines as what you are after - converting the validation rules into Javascript. Unfortunately I am not sure how much effort you need to adapt xVal for your project or rewrite it.

xVal Project可能与您所使用的相同 - 将验证规则转换为Javascript。不幸的是,我不确定您需要多少努力才能使xVal适应您的项目或重写它。

As for running the validations on the annotated classes one can use the Validator (thanks womp)

至于在带注释的类上运行验证,可以使用Validator(感谢womp)