ASP.NET MVC DataAnnotation - 转换为小写

发布于 2024-11-19 02:45:04 字数 623 浏览 2 评论 0原文

我在网站上的登录/注册身份验证方面遇到了一些关于案例方案的问题。

首先,我首先让输入字段的正则表达式仅接受小写。似乎是最明智的做法。但这适得其反。基本上来说,即使我输入文本您必须仅使用小写字母,人们也不会阅读它,会感到沮丧,然后就懒得注册。

因此,为了解决这个问题,我修复了它,以便正则表达式可以同时采用大写和小写值。当我将电子邮件地址存储在数据库中时,我只是将它们转换为小写。每个人都显得很高兴大约8秒钟。

然而,我发现自己现在必须在多个地方这样做。而且它变得越来越令人讨厌。我想知道是否有一种方法可以为我的 ViewModel 制作 DataAnnotation,例如 ..

class LoginViewModel {
   [ConvertLowercase]
   public string Email { get; set; }

   /// ... other view model properties
}

这将始终确保结果在到达我的控制器之前变成小写,从而成为 ViewModel 的整个操作的瓶颈并使其摆脱冗余重复将逻辑放入每个查询、请求和查找中。

有什么建议吗?我已经考虑过构建自己的自定义数据注释,但我只设法理解它的验证过程,而不是实际操作数据。

I am having a bit of a problem with login/register authentication on a site in regards to case scheme.

First, I started out by just having the regular expression for the input fields only accept lowercase. Seemed like the smartest thing to do. But that backfired horribly. Basically put, even if I put in the text You must use only lowercase letters, people don't read it, get frustrated, and then just don't bother to register.

So then, to remedy this, I fixed it so that the regular expression could take both upper and lowercase values. When I stored the email addresses in the database, I just converted them to lowercase. Everyone seemed happy for about 8 seconds.

However I am finding myself having to do this in multiple places, now. And it is getting obnoxious. I was wondering if there was a way to make a DataAnnotation for my ViewModel, like ..

class LoginViewModel {
   [ConvertLowercase]
   public string Email { get; set; }

   /// ... other view model properties
}

That would always ensure that that result turned lowercase before it hits my controller, thus bottlenecking the entire operation at the ViewModel and taking it away from the redundant repetition of putting the logic in every query, request, and lookup.

Any suggestions? I've looked into building my own custom data annotations but I've only managed to understand it as far as validation goes, not actually manipulating data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

但可醉心 2024-11-26 02:45:04

这是一种选择:(创建要传递到控制器/业务层的只读属性)

class LoginViewModel {

     public string Email { get; set; }

     public string LowerCaseEmail  
     {                                    //This is a read only property.
          get { 
                return Eamil.ToLower();
              }
     }

     /// ... other view model properties
  }

here is one option: (creating readonly property which is to be passed to your controller/business layer)

class LoginViewModel {

     public string Email { get; set; }

     public string LowerCaseEmail  
     {                                    //This is a read only property.
          get { 
                return Eamil.ToLower();
              }
     }

     /// ... other view model properties
  }
独闯女儿国 2024-11-26 02:45:04

恕我直言,DataAnnotations 不能也不应该更改数据。这种逻辑应该位于自定义模型绑定器或操作过滤器中。或者,可能更好的是,在将数据保存到数据存储之前先在数据存储库中。

此外,您并不总是需要确保所有情况都正确,确保这一点可能会很痛苦,而不是尝试使用采用 StringComparison 的字符串 Compare、Equals 函数(或 Linq 函数)的重载。 code> 或 StringComparer 并为其提供 OrdinalIgnoreCase 。

IMHO DataAnnotations can't and shouldn't change the data. This kind of logic should go either in custom model binder or in an action filter. Or, probably even better, in the data repository before you save the data to the datastore.

Also, you don't always have to have everything the right case, it can become a pain to ensure that, rather than that try to use overloads of string Compare, Equals function (or Linq functions) that take StringComparison or StringComparer and provide OrdinalIgnoreCase to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文