{"id":619,"date":"2025-12-07T15:37:37","date_gmt":"2025-12-07T07:37:37","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=619"},"modified":"2025-12-07T15:37:37","modified_gmt":"2025-12-07T07:37:37","slug":"%e5%9c%a8-net%e6%a0%87%e5%87%86%e5%ba%93%e4%b8%ad%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e9%aa%8c%e8%af%81%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/07\/%e5%9c%a8-net%e6%a0%87%e5%87%86%e5%ba%93%e4%b8%ad%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e9%aa%8c%e8%af%81%e7%9a%84%e6%96%b9%e6%b3%95\/","title":{"rendered":"\u5728.NET\u6807\u51c6\u5e93\u4e2d\u8fdb\u884c\u6570\u636e\u9a8c\u8bc1\u7684\u65b9\u6cd5"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u51c6\u5907\u4e00\u4efd <strong>\u201c\u5728 .NET \u6807\u51c6\u5e93\u4e2d\u8fdb\u884c\u6570\u636e\u9a8c\u8bc1\u7684\u65b9\u6cd5\uff08\u5b8c\u5168\u6307\u5357\uff09\u201d<\/strong>\uff0c\u7ed3\u6784\u6e05\u6670\u3001\u53ef\u76f4\u63a5\u7528\u4e8e\u6280\u672f\u6587\u7ae0\u3001\u9762\u8bd5\u56de\u7b54\u6216 YouTube \u6280\u672f\u53e3\u64ad\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 \u5728 .NET \u6807\u51c6\u5e93\u4e2d\u8fdb\u884c\u6570\u636e\u9a8c\u8bc1\u7684\u65b9\u6cd5<\/h1>\n\n\n\n<p>(.NET Standard \/ .NET Core \/ .NET 5\u20138 \u5168\u901a\u7528)<\/p>\n\n\n\n<p>\u5728\u4f7f\u7528 .NET \u6784\u5efa\u5e94\u7528\uff08API\u3001\u684c\u9762\u3001\u670d\u52a1\u3001\u5e93\uff09\u65f6\uff0c\u6570\u636e\u9a8c\u8bc1\u662f\u6700\u6838\u5fc3\u7684\u4e00\u6b65\u3002.NET \u6807\u51c6\u5e93\u63d0\u4f9b\u591a\u79cd\u9a8c\u8bc1\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e <strong>\u6a21\u578b\u9a8c\u8bc1 \/ \u624b\u52a8\u9a8c\u8bc1 \/ Fluent \u98ce\u683c \/ Attribute \u7279\u6027\u9a8c\u8bc1<\/strong> \u7b49\u573a\u666f\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u25c6 \u65b9\u6cd5 1\uff1a\u4f7f\u7528 DataAnnotations\uff08\u6700\u5e38\u7528\uff09<\/h1>\n\n\n\n<p>\u8fd9\u662f <strong>.NET \u5185\u7f6e\u7684\u9a8c\u8bc1\u673a\u5236<\/strong>\uff0c\u65e0\u9700\u4efb\u4f55\u7b2c\u4e09\u65b9\u5e93\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. \u6dfb\u52a0\u9a8c\u8bc1\u7279\u6027<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic class UserDto\n{\n    &#x5B;Required]\n    public string Name { get; set; }\n\n    &#x5B;Range(1, 120)]\n    public int Age { get; set; }\n\n    &#x5B;EmailAddress]\n    public string Email { get; set; }\n}\n\n<\/pre><\/div>\n\n\n<p>\u5e38\u7528\u7279\u6027\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Required<\/code><\/li>\n\n\n\n<li><code>StringLength<\/code><\/li>\n\n\n\n<li><code>Range<\/code><\/li>\n\n\n\n<li><code>EmailAddress<\/code><\/li>\n\n\n\n<li><code>Phone<\/code><\/li>\n\n\n\n<li><code>RegularExpression<\/code><\/li>\n\n\n\n<li><code>Compare<\/code><\/li>\n\n\n\n<li><code>MaxLength<\/code> \/ <code>MinLength<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. \u624b\u52a8\u89e6\u53d1\u9a8c\u8bc1\uff08.NET \u6807\u51c6\u5e93\u65b9\u5f0f\uff09<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nvar dto = new UserDto { Name = null, Age = 150 };\nvar results = new List&amp;lt;ValidationResult&gt;();  \n\nbool ok = Validator.TryValidateObject(dto, new ValidationContext(dto), results, true);\n\nif (!ok)\n{\n    foreach (var error in results)\n        Console.WriteLine(error.ErrorMessage);\n}\n\n<\/pre><\/div>\n\n\n<p>\u2714 \u4e0d\u4f9d\u8d56 ASP.NET Core\uff0c\u7eaf\u6807\u51c6\u5e93<br>\u2714 \u53ef\u4ee5\u5728\u4efb\u4f55\u5730\u65b9\u9a8c\u8bc1\uff08Web API \/ WinForm \/ WPF \/ \u63a7\u5236\u53f0 \/ \u670d\u52a1\uff09<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u25c6 \u65b9\u6cd5 2\uff1a\u81ea\u5b9a\u4e49\u5c5e\u6027\u9a8c\u8bc1<\/h1>\n\n\n\n<p>\u5982\u679c DataAnnotations \u4e0d\u591f\u7528\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic class AdultAttribute : ValidationAttribute\n{\n    public override bool IsValid(object value)\n    {\n        if (value is int age)\n            return age &gt;= 18;\n        return false;\n    }\n}\n\n<\/pre><\/div>\n\n\n<p>\u4f7f\u7528\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic class UserDto\n{\n    &#x5B;Adult(ErrorMessage = &quot;Age must be &gt;= 18.&quot;)]\n    public int Age { get; set; }\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u25c6 \u65b9\u6cd5 3\uff1a\u5b9e\u73b0 IValidatableObject\uff08\u590d\u6742\u5bf9\u8c61\u9a8c\u8bc1\uff09<\/h1>\n\n\n\n<p>\u9002\u5408\u8de8\u5b57\u6bb5\u9a8c\u8bc1\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic class RegisterDto : IValidatableObject\n{\n    public string Password { get; set; }\n    public string Confirm { get; set; }\n\n    public IEnumerable&amp;lt;ValidationResult&gt; Validate(ValidationContext validationContext)\n    {\n        if (Password != Confirm)\n        {\n            yield return new ValidationResult(&quot;Passwords do not match&quot;, new&#x5B;] { &quot;Confirm&quot; });\n        }\n    }\n}\n\n<\/pre><\/div>\n\n\n<p>\u2714 \u80fd\u505a\u5bf9\u8c61\u7ea7\u522b\u9a8c\u8bc1<br>\u2714 \u4e0d\u5c40\u9650\u4e8e\u5355\u4e2a\u5b57\u6bb5<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u25c6 \u65b9\u6cd5 4\uff1a\u4f7f\u7528 System.ComponentModel\uff08\u66f4\u81ea\u7531\u7684\u9a8c\u8bc1\uff09<\/h1>\n\n\n\n<p>\u4f60\u4e5f\u53ef\u4ee5\u624b\u52a8\u6784\u5efa\u9a8c\u8bc1\u903b\u8f91\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic static void ValidateAge(int age)\n{\n    if (age &amp;lt; 0 || age &gt; 120)\n        throw new ArgumentOutOfRangeException(nameof(age), &quot;Age must be 0-120&quot;);\n}\n\n<\/pre><\/div>\n\n\n<p>\u9002\u7528\uff1a<br>\u2714 \u6838\u5fc3\u4e1a\u52a1\u903b\u8f91<br>\u2714 \u4e0d\u5e0c\u671b\u4f9d\u8d56 Attribute \u7684\u5e93<br>\u2714 \u81ea\u5b9a\u4e49\u9a8c\u8bc1\u4f53\u7cfb<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u25c6 \u65b9\u6cd5 5\uff1aFluentValidation\uff08\u63a8\u8350\uff1a\u73b0\u4ee3\u3001\u5f3a\u5927\u3001\u4f18\u96c5\uff09<\/h1>\n\n\n\n<p>\u867d\u7136\u4e0d\u662f\u201c\u6807\u51c6\u5e93\u201d\uff0c\u4f46\u4f01\u4e1a\u7ea7\u9879\u76ee\u6700\u5e38\u7528\u3002<\/p>\n\n\n\n<p>\u5b89\u88c5\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndotnet add package FluentValidation\n\n<\/pre><\/div>\n\n\n<p>\u9a8c\u8bc1\u5668\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic class UserValidator : AbstractValidator&amp;lt;UserDto&gt;\n{\n    public UserValidator()\n    {\n        RuleFor(x =&gt; x.Name).NotEmpty();\n        RuleFor(x =&gt; x.Age).InclusiveBetween(1, 120);\n    }\n}\n\n<\/pre><\/div>\n\n\n<p>\u4f7f\u7528\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nvar validator = new UserValidator();\nvar result = validator.Validate(dto);\n\nif (!result.IsValid)\n{\n    foreach (var err in result.Errors)\n        Console.WriteLine(err.ErrorMessage);\n}\n\n<\/pre><\/div>\n\n\n<p>\u4f18\u52bf\uff1a<br>\u2714 \u94fe\u5f0f\u89c4\u5219<br>\u2714 \u5927\u91cf\u5f3a\u5927\u6269\u5c55<br>\u2714 \u5355\u5143\u6d4b\u8bd5\u53cb\u597d<br>\u2714 ASP.NET Core \u81ea\u52a8\u96c6\u6210<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u25c6 \u65b9\u6cd5 6\uff1aTryParse \u6a21\u5f0f\uff08\u57fa\u672c\u7c7b\u578b\u5feb\u901f\u9a8c\u8bc1\uff09<\/h1>\n\n\n\n<p>\u7528\u4e8e\u6570\u5b57\u3001\u65e5\u671f\u3001GUID \u7b49\u9a8c\u8bc1\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nif (!int.TryParse(input, out var value))\n{\n    Console.WriteLine(&quot;Invalid number&quot;);\n}\n\n<\/pre><\/div>\n\n\n<p>\u9002\u7528\uff1a<br>\u2714 \u63a7\u5236\u53f0\u8f93\u5165<br>\u2714 \u6587\u672c\u89e3\u6790<br>\u2714 \u4e0d\u9700\u8981\u6a21\u578b\u7684\u573a\u666f<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u25c6 \u65b9\u6cd5 7\uff1aRegular Expression\uff08\u6b63\u5219\u9a8c\u8bc1\uff09<\/h1>\n\n\n\n<p>\u9002\u7528\u4e8e\u81ea\u5b9a\u4e49\u5b57\u7b26\u4e32\u683c\u5f0f\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nvar pattern = @&quot;^&#x5B;0-9]{6}$&quot;;\nbool ok = Regex.IsMatch(&quot;123456&quot;, pattern);\n\n<\/pre><\/div>\n\n\n<p>\u6216\u7ed3\u5408 DataAnnotation\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;RegularExpression(@&quot;^\\d{6}$&quot;)]\npublic string Code { get; set; }\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u25c6 \u65b9\u6cd5 8\uff1a\u5728 ASP.NET Core \u4e2d\u81ea\u52a8\u542f\u7528\u6a21\u578b\u9a8c\u8bc1<\/h1>\n\n\n\n<p>\u53ea\u8981\u4f60\u5728 model \u4e0a\u52a0 DataAnnotations\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;ApiController]\npublic class UsersController : ControllerBase\n{\n    &#x5B;HttpPost]\n    public IActionResult Create(UserDto dto)\n    {\n        return Ok();\n    }\n}\n\n<\/pre><\/div>\n\n\n<p>ASP.NET Core \u4f1a\u81ea\u52a8\u9a8c\u8bc1\uff0c\u5e76\u8fd4\u56de 400\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udccc \u6700\u7ec8\u5efa\u8bae\uff08\u600e\u4e48\u9009\uff1f\uff09<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u573a\u666f<\/th><th>\u63a8\u8350\u65b9\u5f0f<\/th><\/tr><\/thead><tbody><tr><td>\u7b80\u5355\u5b57\u6bb5\u89c4\u5219<\/td><td>DataAnnotations<\/td><\/tr><tr><td>\u5bf9\u8c61\u7ea7\u3001\u591a\u5b57\u6bb5\u9a8c\u8bc1<\/td><td>IValidatableObject<\/td><\/tr><tr><td>\u4f01\u4e1a\u7ea7\u590d\u6742\u9a8c\u8bc1<\/td><td>FluentValidation<\/td><\/tr><tr><td>\u4e1a\u52a1\u903b\u8f91\u6821\u9a8c\uff08\u65e0\u6a21\u578b\uff09<\/td><td>\u81ea\u5b9a\u4e49 Validator<\/td><\/tr><tr><td>\u5b57\u7b26\u4e32\u683c\u5f0f\u5f3a\u6821\u9a8c<\/td><td>Regex<\/td><\/tr><tr><td>\u8f93\u5165\u89e3\u6790\uff08\u5982\u63a7\u5236\u53f0\uff09<\/td><td>TryParse<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u51c6\u5907\u4e00\u4efd \u201c\u5728 .NET \u6807\u51c6\u5e93\u4e2d\u8fdb\u884c\u6570\u636e\u9a8c\u8bc1\u7684\u65b9\u6cd5\uff08\u5b8c\u5168\u6307\u5357\uff09\u201d\uff0c\u7ed3\u6784&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/07\/%e5%9c%a8-net%e6%a0%87%e5%87%86%e5%ba%93%e4%b8%ad%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e9%aa%8c%e8%af%81%e7%9a%84%e6%96%b9%e6%b3%95\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[],"class_list":["post-619","post","type-post","status-publish","format-standard","hentry","category-asp-net"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=619"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/619\/revisions"}],"predecessor-version":[{"id":620,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/619\/revisions\/620"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}