{"id":367333,"date":"2024-05-21T03:19:01","date_gmt":"2024-05-21T03:19:01","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=367333"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=367333","title":{"rendered":"<span>\u0412\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0445 Minimal API .NET, \u043f\u0440\u043e\u0441\u0442\u043e \u0438 \u0431\u0435\u0437 \u0437\u0430\u0442\u0435\u0439<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041d\u0430\u0447\u043d\u0443 \u0441 \u0440\u0438\u0442\u043e\u0440\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0432\u043e\u043f\u0440\u043e\u0441\u0430 &#8212; \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0443\u0432\u043b\u0435\u043a\u0430\u0442\u0435\u043b\u044c\u043d\u0435\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0438, \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 &#171;\u043d\u0430 \u043b\u0435\u0442\u0443&#187;, \u0430 \u043a\u043b\u0435\u0442\u043e\u0447\u043a\u0438 \u043c\u043e\u0437\u0433\u0430 \u0432\u043e\u0441\u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0442 \u043d\u043e\u0432\u044b\u0435 \u0437\u043d\u0430\u043d\u0438\u044f \u043a\u0430\u043a \u043d\u0435\u0447\u0442\u043e \u0437\u043d\u0430\u043a\u043e\u043c\u043e\u0435, \u043d\u043e \u0441\u043b\u0435\u0433\u043a\u0430 \u043f\u043e\u0434\u0437\u0430\u0431\u044b\u0442\u043e\u0435?  <\/p>\n<p>\u041e\u0442\u0432\u0435\u0442 &#8212; \u0434\u0430, \u0432 \u043e\u0431\u0449\u0435\u043c-\u0442\u043e, \u043c\u043d\u043e\u0433\u043e \u0447\u0435\u0433\u043e!  \u0425\u043e\u0442\u044f, \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u044f, \u043d\u0435\u0441\u043b\u043e\u0436\u043d\u0430\u044f \u0432 \u0441\u0432\u043e\u0451\u043c \u043e\u0441\u0432\u043e\u0435\u043d\u0438\u0438, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043f\u043e\u0437\u0438\u0442\u0438\u0432.  <\/p>\n<p>\u041a\u0430\u043a \u0443\u0436\u0435 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e \u0438\u0437 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f, \u0431\u044b\u0441\u0442\u0440\u0435\u043d\u044c\u043a\u043e \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c\u0441\u044f, \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0444\u0438\u043b\u044c\u0442\u0440\u044b <em>Minimal API<\/em>. \u0420\u0430\u0437\u0443\u043c\u0435\u0435\u0442\u0441\u044f, \u043c\u044b \u043d\u0435 \u0441\u0442\u0430\u043d\u0435\u043c \u0437\u0434\u0435\u0441\u044c \u0443\u043f\u043e\u0434\u043e\u0431\u043b\u044f\u0442\u044c\u0441\u044f \u0438\u0437\u043e\u0431\u0440\u0435\u0442\u0430\u0442\u0435\u043b\u044f\u043c \u043a\u043e\u043b\u0435\u0441\u0430 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u043d\u0430\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u043f\u0430\u043a\u0435\u0442 <a href=\"https:\/\/www.nuget.org\/packages\/FlatValidator\" rel=\"noopener noreferrer nofollow\"><em>FlatValidator<\/em><\/a><em>.<\/em> \u041f\u043e\u0433\u0440\u0443\u0436\u0430\u0442\u044c\u0441\u044f \u0432 \u0434\u0435\u0442\u0430\u043b\u0438 \u043f\u0430\u043a\u0435\u0442\u0430 \u0443 \u043d\u0430\u0441 \u0437\u0430\u0434\u0430\u0447\u0430 \u043d\u0435 \u0441\u0442\u043e\u0438\u0442, \u043c\u044b \u0441\u043a\u043e\u043d\u0446\u0435\u043d\u0442\u0440\u0438\u0440\u0443\u0435\u043c\u0441\u044f \u0432 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u0438.<\/p>\n<p>\u0428\u0430\u0433 \u0432 \u043f\u0440\u043e\u0448\u043b\u043e\u0435. <em>Minimal API<\/em> \u0432\u043f\u0435\u0440\u0432\u044b\u0435 \u0431\u044b\u043b \u0437\u0430\u044f\u0432\u043b\u0435\u043d \u0432 \u0432\u044b\u043f\u0443\u0441\u043a\u0435 <em>.NET 6<\/em>, \u044d\u0442\u043e \u0433\u0438\u0431\u043a\u0430\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u0430\u044f \u0442\u0435\u0445\u043d\u0438\u043a\u0430, \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u0430\u044f \u0434\u043b\u044f \u043e\u0431\u0441\u043b\u0443\u0436\u0438\u0432\u0430\u043d\u0438\u044f <em>HTTP-<\/em>\u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0438\u0437\u0430\u0446\u0438\u0438. \u041f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0435 <em>Minimal API<\/em> \u043f\u043e\u0434\u0432\u0435\u0441\u0438\u043b\u043e \u0432 \u0432\u043e\u0437\u0434\u0443\u0445 \u043e\u0449\u0443\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u043e\u0432 \u043d\u0435\u0443\u043c\u043e\u043b\u0438\u043c\u043e \u0438\u0441\u0442\u0435\u043a\u0430\u0435\u0442. \u0418, \u043a\u0430\u043a \u0432\u0441\u0435\u0433\u0434\u0430, \u043d\u043e\u0432\u044b\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u0430 \u0441\u043e\u0437\u0434\u0430\u044e\u0442 \u043d\u043e\u0432\u044b\u0435 \u0432\u044b\u0437\u043e\u0432\u044b.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u043e\u0431\u043e \u0432\u0441\u0451\u043c \u044d\u0442\u043e\u043c \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0441\u043b\u044b\u0448\u0430\u043b\u0438, \u043d\u0435 \u0441\u0443\u0442\u044c, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043e\u0431\u0441\u0443\u0436\u0434\u0435\u043d\u0438\u0435 \u0432\u043d\u0435\u0441\u0451\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u0443\u044e \u044f\u0441\u043d\u043e\u0441\u0442\u044c, \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u043c \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438\u0441\u0441\u043b\u0435\u0434\u0443\u0435\u043c\u043e\u0433\u043e \u0432\u043e\u043f\u0440\u043e\u0441\u0430.<\/p>\n<p>\u0424\u0438\u043b\u044c\u0442\u0440\u044b \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0445 \u0442\u043e\u0447\u0435\u043a \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b \u0433\u043b\u0430\u0432\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0438\u043c\u0435\u043d\u043d\u043e \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0442\u0435\u0445\u043d\u0438\u043a\u0438 <em>Minimal API<\/em>, \u0445\u043e\u0442\u044f \u0441\u0435\u0439\u0447\u0430\u0441 \u043e\u043d\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0432 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u0438 \u0434\u043b\u044f <em>MVC<\/em>, \u0438 \u0434\u043b\u044f <em>Razor Pages<\/em>. <\/p>\n<p>\u041d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434, \u0444\u0438\u043b\u044c\u0442\u0440\u044b &#8212; \u044d\u0442\u043e \u043d\u0435\u0447\u0442\u043e \u0441\u0440\u0435\u0434\u043d\u0435\u0435 \u043c\u0435\u0436\u0434\u0443 \u0441\u0430\u043c\u0438\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043a \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0439 \u0442\u043e\u0447\u043a\u0435 \u0438 <em>middleware<\/em>. \u0422\u0435\u0440\u043c\u0438\u043d &#8216;<em>middleware<\/em>&#8216; \u0442\u0440\u0443\u0434\u043d\u043e\u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u043c \u043d\u0430 \u0440\u0443\u0441\u0441\u043a\u0438\u0439 \u044f\u0437\u044b\u043a, \u0441\u043a\u0430\u0436\u0435\u043c \u0442\u0430\u043a &#8212; \u044d\u0442\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u044b\u0439 \u043c\u043e\u0434\u0443\u043b\u044c, \u0432\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u043c\u044b\u0439 \u0432 \u0446\u0435\u043f\u043e\u0447\u043a\u0443 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 <em>HTTP-<\/em>\u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u0415\u0441\u043b\u0438 \u0442\u0430\u043a\u0438\u0445 \u043c\u043e\u0434\u0443\u043b\u0435\u0439 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e, \u043e\u043d\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043e\u0434\u0438\u043d \u0437\u0430 \u0434\u0440\u0443\u0433\u0438\u043c. \u041a \u0447\u0435\u043c\u0443 \u044f \u044d\u0442\u043e \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u044e? \u0420\u0430\u043d\u044c\u0448\u0435 \u0434\u043b\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 <em>middleware<\/em>. \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f \u0431\u043e\u043b\u0435\u0435 \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 &#8212; \u0444\u0438\u043b\u044c\u0442\u0440\u044b, \u043a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 `<em>IEndpointFilter<\/em>`.<\/p>\n<p>\u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043a\u043e\u0434 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 `<em>IEndpointFilter<\/em>` \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cs\">public class MyFilter: IEndpointFilter   {       public async ValueTask&lt;object?> InvokeAsync(           EndpointFilterInvocationContext context,           EndpointFilterDelegate next)       {                 var result = await next(context);           if (result is string s)         {             result = s.Replace(\"vodka\", \"pineapple juice\");         }         return result;       } }<\/code><\/pre>\n<p>\u0427\u0442\u043e \u0437\u0434\u0435\u0441\u044c \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442? \u0424\u0438\u043b\u044c\u0442\u0440 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043a \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0439 \u0442\u043e\u0447\u043a\u0435 (\u0432\u044b\u0437\u044b\u0432\u0430\u044f `<em>next()<\/em>`) \u0438 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u0442 \u0432 <em>response<\/em> \u0432\u0441\u0435 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b `<em>vodka<\/em>` \u043d\u0430 `<em>pineapple juice<\/em>`. \u041a\u0430\u043a \u0432\u0438\u0434\u0438\u0442\u0435, \u0432\u0441\u0451 \u043f\u0440\u0435\u0434\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0436\u0435 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c \u044d\u0442\u043e\u0442 \u0444\u0438\u043b\u044c\u0442\u0440 \u043a \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044e <em>Minimal API<\/em>.<\/p>\n<pre><code class=\"cs\">var builder = WebApplication.CreateBuilder(args);      var app = builder.Build();    var group = app       .MapGroup(string.Empty)       .AddEndpointFilter&lt;MyFilter>();  \/\/ &lt;==    group.MapGet(\"\/\", () => \"Hello World\");   group.MapGet(\"\/hi\", () => \"I like to drink vodka!\");      app.Run(); <\/code><\/pre>\n<p>\u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0430\u0448\u0435\u0433\u043e \u0444\u0438\u043b\u044c\u0442\u0440\u0430 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043c\u0435\u043d\u0430 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0444\u0440\u0430\u0437\u044b `<em>I like to drink vodka!<\/em>` \u043d\u0430 `<em>I like to drink pineapple juice!<\/em>`. \u0422\u0430\u043a\u043e\u0439 \u0432\u043e\u0442 \u043d\u0435\u0436\u0434\u0430\u043d\u0447\u0438\u043a \u0434\u043b\u044f \u043b\u044e\u0431\u0438\u0442\u0435\u043b\u044f `<em>vodka<\/em>`. &#171;\u0410 \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c? \u041f\u044c\u044f\u043d\u0441\u0442\u0432\u0443 \u0431\u043e\u0439.&#187;<\/p>\n<p>\u0418\u0442\u0430\u043a, \u0442\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0441 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u043c\u0438 \u0434\u043b\u044f Minimal API \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043b\u0438\u0441\u044c, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0435\u0440\u0435\u0439\u0434\u0451\u043c \u043a \u0431\u043e\u043b\u0435\u0435 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c\u0443 \u043f\u0440\u0438\u043c\u0435\u0440\u0443. \u0427\u0435\u0440\u0435\u0437 <em>NuGet<\/em> \u0438\u043d\u0441\u0442\u0430\u043b\u043b\u0438\u0440\u0443\u0435\u043c \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043f\u0430\u043a\u0435\u0442 <a href=\"https:\/\/www.nuget.org\/packages\/FlatValidator\" rel=\"noopener noreferrer nofollow\"><em>FlatValidator<\/em><\/a>, \u043e\u043d \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043d\u0430\u0448\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u043e\u0444\u0435\u0441\u0441\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e.<\/p>\n<pre><code class=\"bash\">\u276f dotnet add package FlatValidator<\/code><\/pre>\n<p>\u0412 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u043a \u043f\u0430\u043a\u0435\u0442\u0443 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e, \u0447\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u0430 \u043f\u0440\u0435\u0434\u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043e \u0432 \u0434\u0432\u0443\u0445 \u0440\u0435\u0436\u0438\u043c\u0430\u0445, \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u043e\u0447\u043d\u043e \u043d\u0430\u0437\u0432\u0430\u043d\u043d\u044b\u0445 <em>inline<\/em> \u0438 <em>derived<\/em>. <\/p>\n<p>\u0412 <em>inline<\/em>-\u0440\u0435\u0436\u0438\u043c\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0437\u0430\u0434\u0430\u044e\u0442\u0441\u044f \u043f\u0440\u044f\u043c\u043e \u0432 \u0442\u043e\u0447\u043a\u0435 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438. \u042d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0443\u0434\u043e\u0431\u043d\u043e, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0432\u0438\u0434\u0435\u0442\u044c \u043b\u043e\u0433\u0438\u043a\u0443 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 &#171;\u043f\u043e \u043c\u0435\u0441\u0442\u0443&#187;. <\/p>\n<p>\u0421 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u0432 \u0441\u043e\u043b\u0438\u0434\u043d\u044b\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445 \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0433\u043b\u0430\u0441\u043e\u0432\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u0441 \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f\u043c\u0438, \u043e\u0431\u044b\u0447\u043d\u043e \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0443 \u0432\u0441\u0451 \u0436\u0435 \u0432\u044b\u0447\u043b\u0435\u043d\u044f\u044e\u0442 \u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u044e\u0442 \u0432 \u0441\u0442\u0440\u043e\u0433\u043e \u043e\u0442\u0432\u0435\u0434\u0451\u043d\u043d\u044b\u0445 \u043c\u0435\u0441\u0442\u0430\u0445. \u0422\u043e\u0433\u0434\u0430 \u043f\u0440\u043e\u0449\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0441 \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043a\u043b\u0430\u0441\u0441\u0430 `<em>FlatValidator&lt;TModel><\/em>`.<\/p>\n<p>\u041f\u043e\u0437\u0432\u043e\u043b\u044e \u0441\u0435\u0431\u0435 \u0432\u0437\u044f\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0438\u0437\u043c\u0435\u043d\u0451\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0437 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438, \u0437\u0434\u0435\u0441\u044c <em>inline<\/em>-\u0440\u0435\u0436\u0438\u043c :<\/p>\n<pre><code class=\"cs\">\/\/ use asynchronous version var result = await FlatValidator.ValidateAsync(model, v =>  {     \/\/ m == model, IsEmail() is one of funcs for typical data formats     v.ValidIf(m => m.Email.IsEmail(), \"Invalid email\", m => m.Email);      \/\/ involve custom userService for specific logic     v.ErrorIf(async m => await userService.IsUserExistAsync(m.Email),               \"User already registered\", m => m.Email); }); if (!result) \/\/ check the validation result     return TypedResults.ValidationProblem(result.ToDictionary());<\/code><\/pre>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u0438 <em>`ValidIf`<\/em> \u0438 <em>`ErrorIf`<\/em> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0437\u0430\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438. \u0418\u0445 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0443\u0433\u043e\u0434\u043d\u043e \u043c\u043d\u043e\u0433\u043e \u0432 \u043e\u0434\u043d\u043e\u043c \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u0435. `<em>TypedResults.ValidationProblem<\/em>` &#8212; \u044d\u0442\u043e \u0447\u0430\u0441\u0442\u044c <em>.NET 6+<\/em>, \u0443\u043f\u0440\u043e\u0449\u0430\u044e\u0449\u0430\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0442 \u043e\u0448\u0438\u0431\u043e\u043a \u0432 <em>HTTP-<\/em>response.<\/p>\n<p>\u0421\u0430\u043c\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0432 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u0438 <em>inline<\/em>-\u0440\u0435\u0436\u0438\u043c\u0430 \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cs\">var builder = WebApplication.CreateBuilder(args);   var app = builder.Build();      \/\/ Endpoint aka https:\/\/localhost:5000\/todos\/ app.MapPost(\"\/todos\", (Todo todo) =>  {     var result = FlatValidator.Validate(todo, v =>      {         v.ErrorIf(m => m.Title.IsEmpty(), \"Title can not be empty.\", m => m.Title);     });     if (!result)         return TypedResults.ValidationProblem(result.ToDictionary())       \/\/ ....     return Results.Ok(); });    app.Run();   \/\/ Model to test validation functionality public record Todo(string Title, bool IsComplete = false);<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 <em>inline<\/em>-\u0441\u0442\u0438\u043b\u044c \u0432\u0430\u043c \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435\u043c.<\/p>\n<pre><code class=\"cs\">var builder = WebApplication.CreateBuilder(args);   var app = builder.Build();      app.MapPost(\"\/todos\", (Todo todo) =>  {     var result = new TodoValidator().Validate(todo);     if (!result)         return TypedResults.ValidationProblem(result.ToDictionary());       \/\/ ....     return Results.Ok(); });    app.Run();   \/\/ Model to test validation functionality public record Todo(string Title, bool IsComplete = false);  \/\/ Implement custom validator for the model Todo public class TodoValidator : FlatValidator&lt;Todo> {     public TodoValidator()     {         v.ErrorIf(m => m.Title.IsEmpty(), \"Title can not be empty.\", m => m.Title);     } }<\/code><\/pre>\n<p>\u042d\u0442\u0430 \u0437\u0430\u043f\u0438\u0441\u044c \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043e\u043f\u0440\u044f\u0442\u043d\u0435\u0435. \u042f\u0441\u043d\u043e, \u0447\u0442\u043e \u043a\u043b\u0430\u0441\u0441\u044b `<em>Todo<\/em>` \u0438 `<em>TodoValidator<\/em>` \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b \u043b\u0435\u0436\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0432 \u0441\u0432\u043e\u0451\u043c \u0444\u0430\u0439\u043b\u0435.<\/p>\n<p>\u0427\u0442\u043e \u0436, \u0434\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0444\u0438\u043b\u044c\u0442\u0440\u0430 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438, \u0437\u0430\u044f\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0432 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0435 \u0441\u0442\u0430\u0442\u044c\u0438, \u043d\u0430\u043c \u043e\u0441\u0442\u0430\u043b\u0441\u044f \u043e\u0434\u0438\u043d \u0448\u0430\u0433. \u041b\u043e\u0433\u0438\u043a\u0443 \u0432\u044b\u0437\u043e\u0432\u0430 \u0441\u0430\u043c\u043e\u0433\u043e \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u0430 \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0451\u043c \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432\u043d\u0443\u0442\u0440\u044c \u0444\u0438\u043b\u044c\u0442\u0440\u0430.<\/p>\n<pre><code class=\"cs\">app.MapPost(\"\/todos\", (Todo todo) =>  {     return Results.Ok();  }).AddEndpointFilter&lt;ValidationFilter&lt;Todo>>();  \/\/ &lt;==   public class ValidationFilter&lt;T>(IServiceProvider serviceProvider) : IEndpointFilter {     public async ValueTask&lt;object?> InvokeAsync(         EndpointFilterInvocationContext context,          EndpointFilterDelegate next)     {         var validators = serviceProvider.GetServices&lt;IFlatValidator&lt;T>>();         foreach (var validator in validators)         {             if (context.Arguments.FirstOrDefault(x =>                      x?.GetType() == typeof(T)) is not T model)             {                 return TypedResults.Problem(                             detail: \"No approptiate parameter.\",                              statusCode: StatusCodes.Status500InternalServerError);             }              var result = await validator.ValidateAsync(model);             if (!result)               return TypedResults.ValidationProblem(result.ToDictionary());         }          \/\/ call next filter in the chain         return await next(context);     } }<\/code><\/pre>\n<p>\u0417\u0430\u043c\u0435\u0442\u0438\u043b\u0438? \u0422\u0435\u043b\u043e \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0439 \u0442\u043e\u0447\u043a\u0438 `.<em>MapPost(&#171;\/todos&#187;)<\/em>` \u0432\u043e\u043e\u0431\u0449\u0435 \u0438\u0437\u0431\u0430\u0432\u0438\u043b\u043e\u0441\u044c \u043e\u0442 \u043a\u0430\u043a\u0438\u0445-\u043b\u0438\u0431\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u043e\u043a, \u043b\u043e\u0433\u0438\u043a\u0430 \u0442\u0435\u043f\u0435\u0440\u044c \u0432 \u0444\u0438\u043b\u044c\u0442\u0440\u0435. \u041d\u043e \u043d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u043f\u0440\u043e `<em>.AddEndpointFilter&lt;ValidationFilter&lt;Todo>><\/em>`. \u0410, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0444\u0438\u043b\u044c\u0442\u0440 \u0443 \u043d\u0430\u0441 <em>generic<\/em>, \u043e\u043d \u043f\u043e\u0434\u043e\u0439\u0434\u0451\u0442 \u0434\u043b\u044f \u043c\u043e\u0434\u0435\u043b\u0438 \u043b\u044e\u0431\u043e\u0433\u043e \u0442\u0438\u043f\u0430, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0430\u043c \u043a\u043b\u0430\u0441\u0441 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u0430 \u0438 \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 `<em>IServiceCollection<\/em>`.<\/p>\n<pre><code class=\"cs\">\/\/ register a validator for the Todo model builder.Services.AddScoped&lt;IFlatValidator&lt;Todo>, TodoValidator>();<\/code><\/pre>\n<p>\u0412\u043f\u0440\u043e\u0447\u0435\u043c, \u0441 \u044d\u0442\u0438\u043c \u0442\u043e\u0436\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u043d\u0435\u0442. \u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044e \u0432\u0441\u0435\u0445 \u0432\u0430\u0448\u0438\u0445 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u043e\u0432, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u0430\u043a\u0435\u0442 <a href=\"https:\/\/www.nuget.org\/packages\/FlatValidator.DependencyInjection\" rel=\"noopener noreferrer nofollow\">FlatValidator.DependencyInjection<\/a>. <\/p>\n<pre><code class=\"bash\">\u276f dotnet add package FlatValidator.DependencyInjection<\/code><\/pre>\n<pre><code class=\"cs\">var builder = WebApplication.CreateBuilder(args);    builder.Services.AddFlatValidatorsFromAssembly(Assembly.GetExecutingAssembly());<\/code><\/pre>\n<p>\u0412\u043e\u043e\u0431\u0449\u0435 \u0436\u0435, \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0431\u043b\u0438\u0436\u0435 \u043f\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044f\u043c\u0438 \u043f\u0430\u043a\u0435\u0442\u0430 <a href=\"https:\/\/www.nuget.org\/packages\/FlatValidator\" rel=\"noopener noreferrer nofollow\"><em>FlatValidator<\/em><\/a>, \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u0432 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u0433\u043b\u044f\u043d\u0443\u0442\u044c \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0447\u043a\u0443 <a href=\"https:\/\/github.com\/belset\/FlatValidator\" rel=\"noopener noreferrer nofollow\">\u043f\u0440\u043e\u0435\u043a\u0442\u0430<\/a>, \u0442\u0430\u043c \u043d\u0430\u0439\u0434\u0451\u0442\u0441\u044f \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u0438 \u043b\u0435\u0436\u0430\u0442 \u0432\u043f\u043e\u043b\u043d\u0435 \u0433\u043e\u0434\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b. <\/p>\n<p>\u041d\u0435 \u0441\u0435\u043a\u0440\u0435\u0442, \u0447\u0442\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0441\u043b\u043e\u0436\u043d\u043e. \u041d\u0435 \u043c\u0435\u043d\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u043e \u0435\u0433\u043e \u043f\u043e\u0442\u043e\u043c \u0447\u0438\u0442\u0430\u0442\u044c \u0438 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e, \u0438\u0431\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0438 \u043f\u043e\u043d\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u0434\u0430 &#8212; \u043f\u043e\u0440\u043e\u0439 \u0434\u0432\u0430 \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u043e\u043d\u044f\u0442\u0438\u044f. \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 \u0432 \u043f\u0430\u043a\u0435\u0442\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0444\u0430\u043d\u0442\u0430\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u0438 \u0432\u043f\u043e\u043b\u043d\u0435 \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u043c \u043a\u043e\u0434\u0435.<\/p>\n<p>\u041d\u0443 \u0438 \u043d\u0430\u043f\u043e\u0441\u043b\u0435\u0434\u043e\u043a, \u043a\u0430\u043a \u043e\u0431\u0435\u0449\u0430\u043d\u043e, \u0441\u043b\u0430\u0439\u0434\u0438\u043a \u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043f\u0430\u043a\u0435\u0442\u0430. \u0421\u0430\u043c\u0438 benchmark-\u0438 \u0442\u0430\u043a\u0436\u0435 \u043e\u0431\u0440\u0435\u0442\u0430\u044e\u0442\u0441\u044f \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0447\u043a\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u0430, \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u0430\u0447\u0430\u0442\u044c \u0438 \u043f\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/688\/9cf\/db0\/6889cfdb0c52558ab4a0020269140fd0.png\" alt=\"\u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 FlatValidator \u0438 FluentValidation\" title=\"\u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 FlatValidator \u0438 FluentValidation\" width=\"1274\" height=\"352\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/688\/9cf\/db0\/6889cfdb0c52558ab4a0020269140fd0.png\"\/><\/p>\n<div><figcaption>\u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 FlatValidator \u0438 FluentValidation<\/figcaption><\/div>\n<\/figure>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/791466\/\"> https:\/\/habr.com\/ru\/articles\/791466\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041d\u0430\u0447\u043d\u0443 \u0441 \u0440\u0438\u0442\u043e\u0440\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0432\u043e\u043f\u0440\u043e\u0441\u0430 &#8212; \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0443\u0432\u043b\u0435\u043a\u0430\u0442\u0435\u043b\u044c\u043d\u0435\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0438, \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 &#171;\u043d\u0430 \u043b\u0435\u0442\u0443&#187;, \u0430 \u043a\u043b\u0435\u0442\u043e\u0447\u043a\u0438 \u043c\u043e\u0437\u0433\u0430 \u0432\u043e\u0441\u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0442 \u043d\u043e\u0432\u044b\u0435 \u0437\u043d\u0430\u043d\u0438\u044f \u043a\u0430\u043a \u043d\u0435\u0447\u0442\u043e \u0437\u043d\u0430\u043a\u043e\u043c\u043e\u0435, \u043d\u043e \u0441\u043b\u0435\u0433\u043a\u0430 \u043f\u043e\u0434\u0437\u0430\u0431\u044b\u0442\u043e\u0435?  <\/p>\n<p>\u041e\u0442\u0432\u0435\u0442 &#8212; \u0434\u0430, \u0432 \u043e\u0431\u0449\u0435\u043c-\u0442\u043e, \u043c\u043d\u043e\u0433\u043e \u0447\u0435\u0433\u043e!  \u0425\u043e\u0442\u044f, \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u044f, \u043d\u0435\u0441\u043b\u043e\u0436\u043d\u0430\u044f \u0432 \u0441\u0432\u043e\u0451\u043c \u043e\u0441\u0432\u043e\u0435\u043d\u0438\u0438, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043f\u043e\u0437\u0438\u0442\u0438\u0432.  <\/p>\n<p>\u041a\u0430\u043a \u0443\u0436\u0435 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e \u0438\u0437 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f, \u0431\u044b\u0441\u0442\u0440\u0435\u043d\u044c\u043a\u043e \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c\u0441\u044f, \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0444\u0438\u043b\u044c\u0442\u0440\u044b <em>Minimal API<\/em>. \u0420\u0430\u0437\u0443\u043c\u0435\u0435\u0442\u0441\u044f, \u043c\u044b \u043d\u0435 \u0441\u0442\u0430\u043d\u0435\u043c \u0437\u0434\u0435\u0441\u044c \u0443\u043f\u043e\u0434\u043e\u0431\u043b\u044f\u0442\u044c\u0441\u044f \u0438\u0437\u043e\u0431\u0440\u0435\u0442\u0430\u0442\u0435\u043b\u044f\u043c \u043a\u043e\u043b\u0435\u0441\u0430 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u043d\u0430\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u043f\u0430\u043a\u0435\u0442 <a href=\"https:\/\/www.nuget.org\/packages\/FlatValidator\" rel=\"noopener noreferrer nofollow\"><em>FlatValidator<\/em><\/a><em>.<\/em> \u041f\u043e\u0433\u0440\u0443\u0436\u0430\u0442\u044c\u0441\u044f \u0432 \u0434\u0435\u0442\u0430\u043b\u0438 \u043f\u0430\u043a\u0435\u0442\u0430 \u0443 \u043d\u0430\u0441 \u0437\u0430\u0434\u0430\u0447\u0430 \u043d\u0435 \u0441\u0442\u043e\u0438\u0442, \u043c\u044b \u0441\u043a\u043e\u043d\u0446\u0435\u043d\u0442\u0440\u0438\u0440\u0443\u0435\u043c\u0441\u044f \u0432 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u0438.<\/p>\n<p>\u0428\u0430\u0433 \u0432 \u043f\u0440\u043e\u0448\u043b\u043e\u0435. <em>Minimal API<\/em> \u0432\u043f\u0435\u0440\u0432\u044b\u0435 \u0431\u044b\u043b \u0437\u0430\u044f\u0432\u043b\u0435\u043d \u0432 \u0432\u044b\u043f\u0443\u0441\u043a\u0435 <em>.NET 6<\/em>, \u044d\u0442\u043e \u0433\u0438\u0431\u043a\u0430\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u0430\u044f \u0442\u0435\u0445\u043d\u0438\u043a\u0430, \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u0430\u044f \u0434\u043b\u044f \u043e\u0431\u0441\u043b\u0443\u0436\u0438\u0432\u0430\u043d\u0438\u044f <em>HTTP-<\/em>\u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0438\u0437\u0430\u0446\u0438\u0438. \u041f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0435 <em>Minimal API<\/em> \u043f\u043e\u0434\u0432\u0435\u0441\u0438\u043b\u043e \u0432 \u0432\u043e\u0437\u0434\u0443\u0445 \u043e\u0449\u0443\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u043e\u0432 \u043d\u0435\u0443\u043c\u043e\u043b\u0438\u043c\u043e \u0438\u0441\u0442\u0435\u043a\u0430\u0435\u0442. \u0418, \u043a\u0430\u043a \u0432\u0441\u0435\u0433\u0434\u0430, \u043d\u043e\u0432\u044b\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u0430 \u0441\u043e\u0437\u0434\u0430\u044e\u0442 \u043d\u043e\u0432\u044b\u0435 \u0432\u044b\u0437\u043e\u0432\u044b.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u043e\u0431\u043e \u0432\u0441\u0451\u043c \u044d\u0442\u043e\u043c \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0441\u043b\u044b\u0448\u0430\u043b\u0438, \u043d\u0435 \u0441\u0443\u0442\u044c, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043e\u0431\u0441\u0443\u0436\u0434\u0435\u043d\u0438\u0435 \u0432\u043d\u0435\u0441\u0451\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u0443\u044e \u044f\u0441\u043d\u043e\u0441\u0442\u044c, \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u043c \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438\u0441\u0441\u043b\u0435\u0434\u0443\u0435\u043c\u043e\u0433\u043e \u0432\u043e\u043f\u0440\u043e\u0441\u0430.<\/p>\n<p>\u0424\u0438\u043b\u044c\u0442\u0440\u044b \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0445 \u0442\u043e\u0447\u0435\u043a \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b \u0433\u043b\u0430\u0432\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0438\u043c\u0435\u043d\u043d\u043e \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0442\u0435\u0445\u043d\u0438\u043a\u0438 <em>Minimal API<\/em>, \u0445\u043e\u0442\u044f \u0441\u0435\u0439\u0447\u0430\u0441 \u043e\u043d\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0432 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u0438 \u0434\u043b\u044f <em>MVC<\/em>, \u0438 \u0434\u043b\u044f <em>Razor Pages<\/em>. <\/p>\n<p>\u041d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434, \u0444\u0438\u043b\u044c\u0442\u0440\u044b &#8212; \u044d\u0442\u043e \u043d\u0435\u0447\u0442\u043e \u0441\u0440\u0435\u0434\u043d\u0435\u0435 \u043c\u0435\u0436\u0434\u0443 \u0441\u0430\u043c\u0438\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043a \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0439 \u0442\u043e\u0447\u043a\u0435 \u0438 <em>middleware<\/em>. \u0422\u0435\u0440\u043c\u0438\u043d &#8216;<em>middleware<\/em>&#8216; \u0442\u0440\u0443\u0434\u043d\u043e\u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u043c \u043d\u0430 \u0440\u0443\u0441\u0441\u043a\u0438\u0439 \u044f\u0437\u044b\u043a, \u0441\u043a\u0430\u0436\u0435\u043c \u0442\u0430\u043a &#8212; \u044d\u0442\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u044b\u0439 \u043c\u043e\u0434\u0443\u043b\u044c, \u0432\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u043c\u044b\u0439 \u0432 \u0446\u0435\u043f\u043e\u0447\u043a\u0443 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 <em>HTTP-<\/em>\u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u0415\u0441\u043b\u0438 \u0442\u0430\u043a\u0438\u0445 \u043c\u043e\u0434\u0443\u043b\u0435\u0439 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e, \u043e\u043d\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043e\u0434\u0438\u043d \u0437\u0430 \u0434\u0440\u0443\u0433\u0438\u043c. \u041a \u0447\u0435\u043c\u0443 \u044f \u044d\u0442\u043e \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u044e? \u0420\u0430\u043d\u044c\u0448\u0435 \u0434\u043b\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 <em>middleware<\/em>. \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f \u0431\u043e\u043b\u0435\u0435 \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 &#8212; \u0444\u0438\u043b\u044c\u0442\u0440\u044b, \u043a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 `<em>IEndpointFilter<\/em>`.<\/p>\n<p>\u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043a\u043e\u0434 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 `<em>IEndpointFilter<\/em>` \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cs\">public class MyFilter: IEndpointFilter   {       public async ValueTask&lt;object?> InvokeAsync(           EndpointFilterInvocationContext context,           EndpointFilterDelegate next)       {                 var result = await next(context);           if (result is string s)         {             result = s.Replace(\"vodka\", \"pineapple juice\");         }         return result;       } }<\/code><\/pre>\n<p>\u0427\u0442\u043e \u0437\u0434\u0435\u0441\u044c \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442? \u0424\u0438\u043b\u044c\u0442\u0440 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043a \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0439 \u0442\u043e\u0447\u043a\u0435 (\u0432\u044b\u0437\u044b\u0432\u0430\u044f `<em>next()<\/em>`) \u0438 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u0442 \u0432 <em>response<\/em> \u0432\u0441\u0435 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b `<em>vodka<\/em>` \u043d\u0430 `<em>pineapple juice<\/em>`. \u041a\u0430\u043a \u0432\u0438\u0434\u0438\u0442\u0435, \u0432\u0441\u0451 \u043f\u0440\u0435\u0434\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0436\u0435 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c \u044d\u0442\u043e\u0442 \u0444\u0438\u043b\u044c\u0442\u0440 \u043a \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044e <em>Minimal API<\/em>.<\/p>\n<pre><code class=\"cs\">var builder = WebApplication.CreateBuilder(args);      var app = builder.Build();    var group = app       .MapGroup(string.Empty)       .AddEndpointFilter&lt;MyFilter>();  \/\/ &lt;==    group.MapGet(\"\/\", () => \"Hello World\");   group.MapGet(\"\/hi\", () => \"I like to drink vodka!\");      app.Run(); <\/code><\/pre>\n<p>\u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0430\u0448\u0435\u0433\u043e \u0444\u0438\u043b\u044c\u0442\u0440\u0430 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043c\u0435\u043d\u0430 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0444\u0440\u0430\u0437\u044b `<em>I like to drink vodka!<\/em>` \u043d\u0430 `<em>I like to drink pineapple juice!<\/em>`. \u0422\u0430\u043a\u043e\u0439 \u0432\u043e\u0442 \u043d\u0435\u0436\u0434\u0430\u043d\u0447\u0438\u043a \u0434\u043b\u044f \u043b\u044e\u0431\u0438\u0442\u0435\u043b\u044f `<em>vodka<\/em>`. &#171;\u0410 \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c? \u041f\u044c\u044f\u043d\u0441\u0442\u0432\u0443 \u0431\u043e\u0439.&#187;<\/p>\n<p>\u0418\u0442\u0430\u043a, \u0442\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0441 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u043c\u0438 \u0434\u043b\u044f Minimal API \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043b\u0438\u0441\u044c, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0435\u0440\u0435\u0439\u0434\u0451\u043c \u043a \u0431\u043e\u043b\u0435\u0435 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c\u0443 \u043f\u0440\u0438\u043c\u0435\u0440\u0443. \u0427\u0435\u0440\u0435\u0437 <em>NuGet<\/em> \u0438\u043d\u0441\u0442\u0430\u043b\u043b\u0438\u0440\u0443\u0435\u043c \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043f\u0430\u043a\u0435\u0442 <a href=\"https:\/\/www.nuget.org\/packages\/FlatValidator\" rel=\"noopener noreferrer nofollow\"><em>FlatValidator<\/em><\/a>, \u043e\u043d \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043d\u0430\u0448\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u043e\u0444\u0435\u0441\u0441\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e.<\/p>\n<pre><code class=\"bash\">\u276f dotnet add package FlatValidator<\/code><\/pre>\n<p>\u0412 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u043a \u043f\u0430\u043a\u0435\u0442\u0443 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e, \u0447\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u0430 \u043f\u0440\u0435\u0434\u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043e \u0432 \u0434\u0432\u0443\u0445 \u0440\u0435\u0436\u0438\u043c\u0430\u0445, \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u043e\u0447\u043d\u043e \u043d\u0430\u0437\u0432\u0430\u043d\u043d\u044b\u0445 <em>inline<\/em> \u0438 <em>derived<\/em>. <\/p>\n<p>\u0412 <em>inline<\/em>-\u0440\u0435\u0436\u0438\u043c\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0437\u0430\u0434\u0430\u044e\u0442\u0441\u044f \u043f\u0440\u044f\u043c\u043e \u0432 \u0442\u043e\u0447\u043a\u0435 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438. \u042d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0443\u0434\u043e\u0431\u043d\u043e, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0432\u0438\u0434\u0435\u0442\u044c \u043b\u043e\u0433\u0438\u043a\u0443 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 &#171;\u043f\u043e \u043c\u0435\u0441\u0442\u0443&#187;. <\/p>\n<p>\u0421 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u0432 \u0441\u043e\u043b\u0438\u0434\u043d\u044b\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445 \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0433\u043b\u0430\u0441\u043e\u0432\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u0441 \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f\u043c\u0438, \u043e\u0431\u044b\u0447\u043d\u043e \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0443 \u0432\u0441\u0451 \u0436\u0435 \u0432\u044b\u0447\u043b\u0435\u043d\u044f\u044e\u0442 \u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u044e\u0442 \u0432 \u0441\u0442\u0440\u043e\u0433\u043e \u043e\u0442\u0432\u0435\u0434\u0451\u043d\u043d\u044b\u0445 \u043c\u0435\u0441\u0442\u0430\u0445. \u0422\u043e\u0433\u0434\u0430 \u043f\u0440\u043e\u0449\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0441 \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043a\u043b\u0430\u0441\u0441\u0430 `<em>FlatValidator&lt;TModel><\/em>`.<\/p>\n<p>\u041f\u043e\u0437\u0432\u043e\u043b\u044e \u0441\u0435\u0431\u0435 \u0432\u0437\u044f\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0438\u0437\u043c\u0435\u043d\u0451\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0437 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438, \u0437\u0434\u0435\u0441\u044c <em>inline<\/em>-\u0440\u0435\u0436\u0438\u043c :<\/p>\n<pre><code class=\"cs\">\/\/ use asynchronous version var result = await FlatValidator.ValidateAsync(model, v =>  {     \/\/ m == model, IsEmail() is one of funcs for typical data formats     v.ValidIf(m => m.Email.IsEmail(), \"Invalid email\", m => m.Email);      \/\/ involve custom userService for specific logic     v.ErrorIf(async m => await userService.IsUserExistAsync(m.Email),               \"User already registered\", m => m.Email); }); if (!result) \/\/ check the validation result     return TypedResults.ValidationProblem(result.ToDictionary());<\/code><\/pre>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u0438 <em>`ValidIf`<\/em> \u0438 <em>`ErrorIf`<\/em> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0437\u0430\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438. \u0418\u0445 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0443\u0433\u043e\u0434\u043d\u043e \u043c\u043d\u043e\u0433\u043e \u0432 \u043e\u0434\u043d\u043e\u043c \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u0435. `<em>TypedResults.ValidationProblem<\/em>` &#8212; \u044d\u0442\u043e \u0447\u0430\u0441\u0442\u044c <em>.NET 6+<\/em>, \u0443\u043f\u0440\u043e\u0449\u0430\u044e\u0449\u0430\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0442 \u043e\u0448\u0438\u0431\u043e\u043a \u0432 <em>HTTP-<\/em>response.<\/p>\n<p>\u0421\u0430\u043c\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0432 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u0438 <em>inline<\/em>-\u0440\u0435\u0436\u0438\u043c\u0430 \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cs\">var builder = WebApplication.CreateBuilder(args);   var app = builder.Build();      \/\/ Endpoint aka https:\/\/localhost:5000\/todos\/ app.MapPost(\"\/todos\", (Todo todo) =>  {     var result = FlatValidator.Validate(todo, v =>      {         v.ErrorIf(m => m.Title.IsEmpty(), \"Title can not be empty.\", m => m.Title);     });     if (!result)         return TypedResults.ValidationProblem(result.ToDictionary())       \/\/ ....     return Results.Ok(); });    app.Run();   \/\/ Model to test validation functionality public record Todo(string Title, bool IsComplete = false);<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 <em>inline<\/em>-\u0441\u0442\u0438\u043b\u044c \u0432\u0430\u043c \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435\u043c.<\/p>\n<pre><code class=\"cs\">var builder = WebApplication.CreateBuilder(args);   var app = builder.Build();      app.MapPost(\"\/todos\", (Todo todo) =>  {     var result = new TodoValidator().Validate(todo);     if (!result)         return TypedResults.ValidationProblem(result.ToDictionary());       \/\/ ....     return Results.Ok(); });    app.Run();   \/\/ Model to test validation functionality public record Todo(string Title, bool IsComplete = false);  \/\/ Implement custom validator for the model Todo public class TodoValidator : FlatValidator&lt;Todo> {     public TodoValidator()     {         v.ErrorIf(m => m.Title.IsEmpty(), \"Title can not be empty.\", m => m.Title);     } }<\/code><\/pre>\n<p>\u042d\u0442\u0430 \u0437\u0430\u043f\u0438\u0441\u044c \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043e\u043f\u0440\u044f\u0442\u043d\u0435\u0435. \u042f\u0441\u043d\u043e, \u0447\u0442\u043e \u043a\u043b\u0430\u0441\u0441\u044b `<em>Todo<\/em>` \u0438 `<em>TodoValidator<\/em>` \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b \u043b\u0435\u0436\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0432 \u0441\u0432\u043e\u0451\u043c \u0444\u0430\u0439\u043b\u0435.<\/p>\n<p>\u0427\u0442\u043e \u0436, \u0434\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0444\u0438\u043b\u044c\u0442\u0440\u0430 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438, \u0437\u0430\u044f\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0432 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0435 \u0441\u0442\u0430\u0442\u044c\u0438, \u043d\u0430\u043c \u043e\u0441\u0442\u0430\u043b\u0441\u044f \u043e\u0434\u0438\u043d \u0448\u0430\u0433. \u041b\u043e\u0433\u0438\u043a\u0443 \u0432\u044b\u0437\u043e\u0432\u0430 \u0441\u0430\u043c\u043e\u0433\u043e \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u0430 \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0451\u043c \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432\u043d\u0443\u0442\u0440\u044c \u0444\u0438\u043b\u044c\u0442\u0440\u0430.<\/p>\n<pre><code class=\"cs\">app.MapPost(\"\/todos\", (Todo todo) =>  {     return Results.Ok();  }).AddEndpointFilter&lt;ValidationFilter&lt;Todo>>();  \/\/ &lt;==   public class ValidationFilter&lt;T>(IServiceProvider serviceProvider) : IEndpointFilter {     public async ValueTask&lt;object?> InvokeAsync(         EndpointFilterInvocationContext context,          EndpointFilterDelegate next)     {         var validators = serviceProvider.GetServices&lt;IFlatValidator&lt;T>>();         foreach (var validator in validators)         {             if (context.Arguments.FirstOrDefault(x =>                      x?.GetType() == typeof(T)) is not T model)             {                 return TypedResults.Problem(                             detail: \"No approptiate parameter.\",                              statusCode: StatusCodes.Status500InternalServerError);             }              var result = await validator.ValidateAsync(model);             if (!result)               return TypedResults.ValidationProblem(result.ToDictionary());         }          \/\/ call next filter in the chain         return await next(context);     } }<\/code><\/pre>\n<p>\u0417\u0430\u043c\u0435\u0442\u0438\u043b\u0438? \u0422\u0435\u043b\u043e \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0439 \u0442\u043e\u0447\u043a\u0438 `.<em>MapPost(&#171;\/todos&#187;)<\/em>` \u0432\u043e\u043e\u0431\u0449\u0435 \u0438\u0437\u0431\u0430\u0432\u0438\u043b\u043e\u0441\u044c \u043e\u0442 \u043a\u0430\u043a\u0438\u0445-\u043b\u0438\u0431\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u043e\u043a, \u043b\u043e\u0433\u0438\u043a\u0430 \u0442\u0435\u043f\u0435\u0440\u044c \u0432 \u0444\u0438\u043b\u044c\u0442\u0440\u0435. \u041d\u043e \u043d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u043f\u0440\u043e `<em>.AddEndpointFilter&lt;ValidationFilter&lt;Todo>><\/em>`. \u0410, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0444\u0438\u043b\u044c\u0442\u0440 \u0443 \u043d\u0430\u0441 <em>generic<\/em>, \u043e\u043d \u043f\u043e\u0434\u043e\u0439\u0434\u0451\u0442 \u0434\u043b\u044f \u043c\u043e\u0434\u0435\u043b\u0438 \u043b\u044e\u0431\u043e\u0433\u043e \u0442\u0438\u043f\u0430, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0430\u043c \u043a\u043b\u0430\u0441\u0441 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u0430 \u0438 \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 `<em>IServiceCollection<\/em>`.<\/p>\n<pre><code class=\"cs\">\/\/ register a validator for the Todo model builder.Services.AddScoped&lt;IFlatValidator&lt;Todo>, TodoValidator>();<\/code><\/pre>\n<p>\u0412\u043f\u0440\u043e\u0447\u0435\u043c, \u0441 \u044d\u0442\u0438\u043c \u0442\u043e\u0436\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u043d\u0435\u0442. \u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044e \u0432\u0441\u0435\u0445 \u0432\u0430\u0448\u0438\u0445 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u043e\u0432, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u0430\u043a\u0435\u0442 <a href=\"https:\/\/www.nuget.org\/packages\/FlatValidator.DependencyInjection\" rel=\"noopener noreferrer nofollow\">FlatValidator.DependencyInjection<\/a>. <\/p>\n<pre><code class=\"bash\">\u276f dotnet add package FlatValidator.DependencyInjection<\/code><\/pre>\n<pre><code class=\"cs\">var builder = WebApplication.CreateBuilder(args);    builder.Services.AddFlatValidatorsFromAssembly(Assembly.GetExecutingAssembly());<\/code><\/pre>\n<p>\u0412\u043e\u043e\u0431\u0449\u0435 \u0436\u0435, \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0431\u043b\u0438\u0436\u0435 \u043f\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044f\u043c\u0438 \u043f\u0430\u043a\u0435\u0442\u0430 <a href=\"https:\/\/www.nuget.org\/packages\/FlatValidator\" rel=\"noopener noreferrer nofollow\"><em>FlatValidator<\/em><\/a>, \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u0432 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u0433\u043b\u044f\u043d\u0443\u0442\u044c \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0447\u043a\u0443 <a href=\"https:\/\/github.com\/belset\/FlatValidator\" rel=\"noopener noreferrer nofollow\">\u043f\u0440\u043e\u0435\u043a\u0442\u0430<\/a>, \u0442\u0430\u043c \u043d\u0430\u0439\u0434\u0451\u0442\u0441\u044f \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u0438 \u043b\u0435\u0436\u0430\u0442 \u0432\u043f\u043e\u043b\u043d\u0435 \u0433\u043e\u0434\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b. <\/p>\n<p>\u041d\u0435 \u0441\u0435\u043a\u0440\u0435\u0442, \u0447\u0442\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0441\u043b\u043e\u0436\u043d\u043e. \u041d\u0435 \u043c\u0435\u043d\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u043e \u0435\u0433\u043e \u043f\u043e\u0442\u043e\u043c \u0447\u0438\u0442\u0430\u0442\u044c \u0438 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e, \u0438\u0431\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0438 \u043f\u043e\u043d\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u0434\u0430 &#8212; \u043f\u043e\u0440\u043e\u0439 \u0434\u0432\u0430 \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u043e\u043d\u044f\u0442\u0438\u044f. \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 \u0432 \u043f\u0430\u043a\u0435\u0442\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0444\u0430\u043d\u0442\u0430\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u0438 \u0432\u043f\u043e\u043b\u043d\u0435 \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u043c \u043a\u043e\u0434\u0435.<\/p>\n<p>\u041d\u0443 \u0438 \u043d\u0430\u043f\u043e\u0441\u043b\u0435\u0434\u043e\u043a, \u043a\u0430\u043a \u043e\u0431\u0435\u0449\u0430\u043d\u043e, \u0441\u043b\u0430\u0439\u0434\u0438\u043a \u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043f\u0430\u043a\u0435\u0442\u0430. \u0421\u0430\u043c\u0438 benchmark-\u0438 \u0442\u0430\u043a\u0436\u0435 \u043e\u0431\u0440\u0435\u0442\u0430\u044e\u0442\u0441\u044f \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0447\u043a\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u0430, \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u0430\u0447\u0430\u0442\u044c \u0438 \u043f\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c.<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 FlatValidator \u0438 FluentValidation<\/figcaption><\/div>\n<\/figure>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/791466\/\"> https:\/\/habr.com\/ru\/articles\/791466\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-367333","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/367333","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=367333"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/367333\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=367333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=367333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=367333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}