简体   繁体   English

使用WebAPI的Webforms C#客户端验证CustomValidator

[英]Webforms C# client side validation CustomValidator using WebAPI

Basically I want to write my validation logic once and use it on both server and client side. 基本上,我只想编写一次验证逻辑,然后在服务器端和客户端都使用它。

It seems like http://www.asp.net/web-forms/tutorials/hands-on-labs/whats-new-in-web-forms-in-aspnet-45 would fit the bill, but there does not appear to be support for a plain text input so..... 看来http://www.asp.net/web-forms/tutorials/hands-on-labs/whats-new-in-web-forms-in-aspnet-45可以满足要求,但没有出现为了支持纯文本输入,所以.....

I am using C# and writing a webforms 4.5 application backed by entity framework 5. What I am looking at is how to provide validation client side and server side without repeating the validation logic. 我正在使用C#并编写由实体框架5支持的webforms 4.5应用程序。我正在研究的是如何在不重复验证逻辑的情况下提供验证客户端和服务器端。

Say I have a text intput that I want to validate for min length, existence, max length. 说我有一个文本输入,我想验证最小长度,存在性,最大长度。 I could put a custom validator on the form and write some javascript code but then I would have to code something similar in the back end as well. 我可以在表单上放置一个自定义验证器,并编写一些javascript代码,但随后我也必须在后端编写类似的代码。

What seems like a better approach would be to encapsulate the validation logic in the back end and then call that logic via a HTTP GET from JS. 似乎更好的方法是将验证逻辑封装在后端,然后通过JS的HTTP GET调用该逻辑。 That way if I need to change the validation logic I update it once in the code behind and the JS client side would stay the same as its merely calling the same GET. 这样,如果我需要更改验证逻辑,我将在后面的代码中对其进行一次更新,并且JS客户端将保持与仅调用相同GET相同。

I have seen various examples relating to MVC / Web One / Web API but cannot see how I can apply this to what I am proposing. 我已经看到了与MVC / Web One / Web API有关的各种示例,但是看不到如何将其应用于我的建议中。

Has anyone got any suggestions? 有没有人有任何建议? Has this been done before? 以前做过吗? I don't want to reimplement the wheel if I don't have to. 如果不需要,我不想重新实现。

ASP.NET does this for you! ASP.NET为您做到了! You can use annotations in your model classes, like: 您可以在模型类中使用批注,例如:

 [Required, StringLength(100), Display(Name = "Name")]
 public string ProductName { get; set; }

See this tutorial: http://www.asp.net/web-forms/tutorials/aspnet-45/getting-started-with-aspnet-45-web-forms/create_the_data_access_layer 请参阅本教程: http : //www.asp.net/web-forms/tutorials/aspnet-45/getting-started-with-aspnet-45-web-forms/create_the_data_access_layer

There are plenty of annotations you can use, and they will work for client-side validation too. 您可以使用很多注释,它们也可以用于客户端验证。 So you only have to "write" the logic once. 因此,您只需“编写”一次逻辑。

If standard annotations are not sufficient, you can declare your own annotations. 如果标准注释不足,则可以声明自己的注释。 But then you also have to write a client-side validation. 但是然后,您还必须编写客户端验证。 This is indeed problematic. 这确实是有问题的。 Making GET-Calls to the server for validation is of course possible, but this would not be a "real" client-side validation. 当然可以对服务器进行GET调用以进行验证,但这不是“真正的”客户端验证。 In these cases maintaining the logic twice might be the most practical solution. 在这些情况下,两次维护逻辑可能是最实际的解决方案。

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

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