﻿
{"id":23014,"date":"2021-09-19T01:18:22","date_gmt":"2021-09-19T01:18:22","guid":{"rendered":"https:\/\/www.gencayyildiz.com\/blog\/?p=23014"},"modified":"2021-09-19T01:18:22","modified_gmt":"2021-09-19T01:18:22","slug":"asp-net-core-web-api-custom-formattersozel-bicimlendiriciler","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/asp-net-core-web-api-custom-formattersozel-bicimlendiriciler\/","title":{"rendered":"Asp.NET Core Web API &#8211; Custom Formatters(\u00d6zel Bi\u00e7imlendiriciler)"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Asp.NET Core Web API uygulamalar\u0131, client&#8217;lar ve di\u011fer uygulamalar ile JSON, XML veya d\u00fcz\/sade metin gibi dahili\/mevcut bi\u00e7imleri kullanarak ileti\u015fim kurarlar. Bizler Asp.NET Core uygulamalar\u0131nda varsay\u0131lan olarak desteklenen JSON, XML ve d\u00fcz metin yerine \u00f6zel bi\u00e7imlendiricileri(custom formatters) kullanarak kendimize ait bir ileti\u015fim format standard\u0131 olu\u015fturabilir ve uygulamalar\u0131m\u0131zda bunu esas olarak kullanabiliriz. Bu i\u00e7eri\u011fimizde Asp.NET Core mimarisinde \u00d6zel Bi\u00e7imlendiricilerin(Custom Formatters) nas\u0131l ve hangi prensiplerle olu\u015fturulaca\u011f\u0131n\u0131 inceleyecek ve taraf\u0131m\u0131zca geli\u015ftirilmi\u015f dahili olarak desteklenmeyen \u00f6zel bir ileti\u015fim t\u00fcr\u00fcn\u00fcn nas\u0131l kullan\u0131labilir hale getirildi\u011fini g\u00f6rece\u011fiz.<\/p>\n<p>\u00d6ncelikle \u00f6zel bi\u00e7imlendirici olu\u015fturman\u0131n \u00fc\u00e7 temel ad\u0131m\u0131n\u0131 tan\u0131mlayarak ba\u015flayal\u0131m&#8230;<\/p>\n<ol>\n<li><em><strong>\u00c7\u0131kt\u0131 Bi\u00e7imlendirici &#8211; Output Formatter<\/strong><\/em><br \/>\nVerileri client&#8217;a g\u00f6ndermek ve serile\u015ftirmek i\u00e7in output formatter olu\u015fturulur.\n<\/li>\n<li><em><strong>Girdi Bi\u00e7imlendirici &#8211; Input Formatter<\/strong><\/em><br \/>\nClient&#8217;tan al\u0131nan verilerin deserialize s\u00fcre\u00e7leri i\u00e7in input formatter olu\u015fturulur.\n<\/li>\n<li><em><strong>Bi\u00e7imlendiricilerin Uygulamaya Eklenmesi<\/strong><\/em><br \/>\nOlu\u015fturulan formatter&#8217;lar\u0131n uygulama taraf\u0131ndan varsay\u0131lan olarak kullan\u0131labilmesi i\u00e7in Asp.NET Core temel konfig\u00fcrasyonlar\u0131na eklenir.\n<\/li>\n<\/ol>\n<h5>\u00d6zel Bi\u00e7imlendirici Olu\u015fturma<\/h5>\n<p>\u015eimdide teknik a\u00e7\u0131dan Asp.NET Core uygulamas\u0131 i\u00e7in \u00f6zel bi\u00e7imlendirici olu\u015fturman\u0131n temel ad\u0131mlar\u0131n\u0131 tan\u0131mlayal\u0131m&#8230;<\/p>\n<ol>\n<li>\u00d6zel bi\u00e7imlendiriciyi temsil edecek bir s\u0131n\u0131f olu\u015fturulur.<\/li>\n<li>Ard\u0131ndan bu s\u0131n\u0131f <code style=\"font-size:13px;\">XmlDataContractSerializerInputFormatter<\/code>, <code style=\"font-size:13px;\">TextInputFormatter<\/code>, <code style=\"font-size:13px;\">JsonInputFormatter<\/code> vb. gibi bir temel\/base s\u0131n\u0131ftan t\u00fcretilir. Bu s\u0131n\u0131flar girdi bi\u00e7imlendiricileri i\u00e7in kullan\u0131labilen bir\u00e7ok dahili bi\u00e7imlendiricilerden sadece birka\u00e7\u0131d\u0131r. Bunlar\u0131n d\u0131\u015f\u0131nda <code style=\"font-size:13px;\">XmlDataContractSerializerOutputFormatter<\/code>, <code style=\"font-size:13px;\">TextOutputFormatter<\/code>, <code style=\"font-size:13px;\">JsonOutputFormatter<\/code> vb. gibi bir\u00e7ok yerle\u015fik \u00e7\u0131kt\u0131 bi\u00e7imlendiricilerde mevcuttur. Haz\u0131r laf\u0131 gelmi\u015fken <code style=\"font-size:13px;\">InputFormatter<\/code> t\u00fcr\u00fc t\u00fcm girdi bi\u00e7imlendiricilerinin temel s\u0131n\u0131f\u0131d\u0131r ve ayr\u0131ca t\u00fcm giri\u015f bi\u00e7imlendiriciler <code style=\"font-size:13px;color:orange;\">IInputFormatter<\/code> aray\u00fcz\u00fcn\u00fc uygular.<\/li>\n<li>Olu\u015fturulan s\u0131n\u0131f i\u00e7erisinde ge\u00e7erli medya t\u00fcrleri ve kodlamalar tasarlan\u0131r.<\/li>\n<\/ol>\n<p>\u015eimdi gelin \u00f6rnek olarak <code style=\"font-size:13px;\">TextInputFormatter<\/code> ve <code style=\"font-size:13px;\">TextOutputFormatter<\/code> s\u0131n\u0131flar\u0131ndan t\u00fcreyen iki \u00f6zelle\u015ftirici s\u0131n\u0131f olu\u015ftural\u0131m. \u0130lk olarak <code style=\"font-size:13px;\">TextOutputFormatter<\/code>&#8216;dan t\u00fcreyecek olan s\u0131n\u0131f ile ba\u015flayal\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class UserOutputFormatter : TextOutputFormatter\r\n    {\r\n        public UserOutputFormatter()\r\n        {\r\n            SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse(&quot;text\/customFormat&quot;));\r\n\r\n            SupportedEncodings.Add(Encoding.UTF8);\r\n            SupportedEncodings.Add(Encoding.Unicode);\r\n        }\r\n        protected override bool CanWriteType(Type type)\r\n        {\r\n            return typeof(User).IsAssignableFrom(type) || typeof(IEnumerable&lt;User&gt;).IsAssignableFrom(type);\r\n        }\r\n        public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)\r\n        {\r\n            IServiceProvider serviceProvider = context.HttpContext.RequestServices;\r\n            HttpResponse response = context.HttpContext.Response;\r\n\r\n            StringBuilder buffer = new();\r\n            if (context.Object is IEnumerable&lt;User&gt;)\r\n                foreach (User user in context.Object as IEnumerable&lt;User&gt;)\r\n                    FormatData(buffer, user);\r\n            else\r\n                FormatData(buffer, context.Object as User);\r\n\r\n            await response.WriteAsync(buffer.ToString());\r\n        }\r\n\r\n        void FormatData(StringBuilder stringBuilder, User user)\r\n        {\r\n            stringBuilder.AppendLine(&quot;****&quot;);\r\n            stringBuilder.Append(&quot;Employee : &quot;);\r\n            stringBuilder.Append(user.Name);\r\n            stringBuilder.Append(&quot; &quot;);\r\n            stringBuilder.AppendLine(user.Surname);\r\n        }\r\n    }\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funu incelerseniz e\u011fer &#8216;TextOutputFormatter&#8217; abstract class&#8217;\u0131ndan t\u00fcreyen &#8216;UserOutputFormatter &#8216; ad\u0131nda bir s\u0131n\u0131f olu\u015fturulmu\u015ftur. Olu\u015fturulan bu s\u0131n\u0131f\u0131n constructor&#8217;\u0131na g\u00f6z atarsan\u0131z e\u011fer &#8216;SupportedMediaTypes&#8217; ve &#8216;SupportedEncodings&#8217; koleksiyonlar\u0131 arac\u0131l\u0131\u011f\u0131yla ge\u00e7erli media t\u00fcr\u00fc ve kodlamalar belirtilmektedir. &#8216;CanWriteType&#8217; metodunda serilize edilecek olan datan\u0131n t\u00fcr kontrol\u00fc ger\u00e7ekle\u015ftirilmektedir. Yani output olarak g\u00f6nderilecek datan\u0131n &#8216;User&#8217; ya da &#8216;List&lt;User&gt;&#8217; olup olmad\u0131\u011f\u0131 denetlenmektedir. &#8216;WriteResponseBodyAsync&#8217; metodunda ise \u00f6zel bi\u00e7ime uygun serilizasyon operasyonu ger\u00e7ekle\u015ftirilmektedir.<\/p>\n<p>Baz\u0131 durumlarda &#8216;CanWriteType&#8217; metodu yerine &#8216;CanWriteResult&#8217; metodunu kullanmam\u0131z gerekebilir. Bu;<\/p>\n<ul>\n<li>Action&#8217;\u0131n model s\u0131n\u0131f\u0131 d\u00f6nd\u00fcrmesi gerekti\u011fi,<\/li>\n<li>Runtime&#8217;da derived class&#8217;\u0131n d\u00f6nd\u00fcr\u00fclmesi gerekti\u011fi,<\/li>\n<li>Runtime&#8217;da Action taraf\u0131ndan hangi derived class&#8217;\u0131n d\u00f6nd\u00fcr\u00fcld\u00fc\u011f\u00fcn\u00fcn bilinmesi gerekti\u011fi<\/li>\n<\/ul>\n<p>durumlar olarak ifade edilebilir.<\/p>\n<p>Bu durumu \u015f\u00f6yle \u00f6rneklendirebiliriz. &#8216;User&#8217; nesnesinin &#8216;Employee&#8217; ve &#8216;Customer&#8217; s\u0131n\u0131flar\u0131n\u0131n base class&#8217;\u0131 oldu\u011funu varsayal\u0131m. Normal \u015fartlarda yukar\u0131daki gibi &#8216;CanWriteType&#8217; metodu ile t\u00fcr kontrol\u00fc yaparsak &#8216;Employee&#8217; ve &#8216;Customer&#8217; nesnelerinin her ikisi de &#8216;User&#8217; nesnesinden t\u00fcreyece\u011fi i\u00e7in \u00f6zel bi\u00e7imlendirici taraf\u0131ndan \u015fekillendirilecek ve formatland\u0131r\u0131lacakt\u0131r. Ancak bizler burada \u00f6zel bi\u00e7imlendirici a\u00e7\u0131s\u0131ndan &#8216;Customer&#8217; nesnenin \u00e7al\u0131\u015fmas\u0131n\u0131 istemeyebilir ve sadece &#8216;Employee&#8217; nesnelerinde devreye girmesini isteyebiliriz. \u0130\u015fte b\u00f6yle bir durumda &#8216;CanWriteType&#8217; metodundan ziyade &#8216;CanWriteResult&#8217; metodu devreye girecek ve i\u00e7erisinde alm\u0131\u015f oldu\u011fu &#8216;OutputFormatterCanWriteContext&#8217; t\u00fcr\u00fcnden parametredeki &#8216;Object&#8217; property&#8217;si \u00fczerinden bu ayr\u0131m ger\u00e7ekle\u015ftirilebilecek. <em style=\"font-size:13px;\">Peki &#8216;Customer&#8217; nesnesi geldi\u011finde hangi bi\u00e7imde sonu\u00e7 d\u00f6necek?<\/em> diye sordu\u011funuzu duyar gibiyim&#8230; Tabi ki de varsay\u0131lan bi\u00e7imlendirme t\u00fcr\u00fc olan JSON t\u00fcr\u00fcnde sonu\u00e7 \u00fcretilip, d\u00f6nd\u00fcr\u00fclecektir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class UserOutputFormatter : TextOutputFormatter\r\n    {\r\n        .\r\n        .\r\n        .\r\n        public override bool CanWriteResult(OutputFormatterCanWriteContext context)\r\n        {\r\n            return\r\n                ((context.Object is Employee || context.Object is List&lt;Employee&gt;)\r\n                    ||\r\n                (context.Object is User || context.Object is List&lt;User&gt;))\r\n                        &amp;&amp;\r\n                (context.Object is not Customer || context.Object is not List&lt;Customer&gt;);\r\n        }\r\n        .\r\n        .\r\n        .\r\n    }\r\n<\/pre>\n<p>Bak\u0131n! Yukar\u0131daki bi\u00e7imlendiricinin &#8216;User&#8217; ve &#8216;Employee&#8217; nesnelerinde \u00e7al\u0131\u015fabilirli\u011fini ay\u0131rt edebilmek i\u00e7in &#8216;CanWriteType&#8217; yerine &#8216;CanWriteResult&#8217; metodunun override edilerek kullan\u0131ld\u0131\u011f\u0131na dikkatinizi \u00e7ekerim.<\/p>\n<p>Velhas\u0131l, art\u0131k geli\u015ftirilen bu bi\u00e7imlendiriciyi Asp.NET Core uygulamas\u0131na dahil etmemiz gerekmektedir. Bunun i\u00e7in ilgili uygulaman\u0131n &#8216;Startup.cs&#8217; dosyas\u0131ndaki &#8216;ConfigureServices&#8217; metodunda a\u015fa\u011f\u0131daki \u00e7al\u0131\u015fman\u0131n yap\u0131lmas\u0131 yeterli olacakt\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class Startup\r\n    {\r\n        .\r\n        .\r\n        .\r\n        public void ConfigureServices(IServiceCollection services)\r\n        {\r\n            .\r\n            .\r\n            .\r\n            services.AddControllers(options =&gt;\r\n            {\r\n                options.OutputFormatters.Insert(0, new UserOutputFormatter());\r\n                options.FormatterMappings.SetMediaTypeMappingForFormat(&quot;customFormat&quot;, MediaTypeHeaderValue.Parse(&quot;text\/customFormat&quot;));\r\n            });\r\n            .\r\n            .\r\n            .\r\n        }\r\n        .\r\n        .\r\n        .\r\n    }\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funu incelerseniz e\u011fer &#8216;AddController&#8217; metodu i\u00e7erisinde &#8216;OutputFormatters&#8217; \u00f6zelli\u011fine ilgili bi\u00e7imlendirici eklenmektedir. <em style=\"font-size:13px;\">Neden OutputFormatters?<\/em> diye sorarsan\u0131z e\u011fer olu\u015fturdu\u011fumuz bi\u00e7imlendirici bir output\/\u00e7\u0131kt\u0131 bi\u00e7imlendiriciydi de ondan!<\/p>\n<p>\u015eimdi &#8216;UsersController.cs&#8217; ad\u0131nda bir controller olu\u015ftural\u0131m ve i\u00e7eri\u011fini a\u015fa\u011f\u0131daki gibi doldural\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    &#x5B;Route(&quot;api\/&#x5B;controller]&quot;)]\r\n    &#x5B;ApiController]\r\n    public class UsersController : ControllerBase\r\n    {\r\n        &#x5B;HttpGet(&quot;&#x5B;action]&quot;)]\r\n        public IActionResult GetUsers()\r\n        {\r\n            List&lt;User&gt; employees = new()\r\n            {\r\n                new() { Age = 30, Name = &quot;Gen\u00e7ay&quot;, Surname = &quot;Y\u0131ld\u0131z&quot; },\r\n                new() { Age = 35, Name = &quot;Mehmet&quot;, Surname = &quot;C\u00fcmb\u00fcl&quot; },\r\n                new() { Age = 40, Name = &quot;Mehmet&quot;, Surname = &quot;Sakarya&quot; }\r\n            };\r\n\r\n            return Ok(employees);\r\n        }\r\n\r\n        &#x5B;HttpGet(&quot;&#x5B;action]&quot;)]\r\n        public IActionResult GetEmployees()\r\n        {\r\n            List&lt;Employee&gt; employees = new()\r\n            {\r\n                new() { Age = 30, Name = &quot;Gen\u00e7ay&quot;, Surname = &quot;Y\u0131ld\u0131z&quot; },\r\n                new() { Age = 35, Name = &quot;Mehmet&quot;, Surname = &quot;C\u00fcmb\u00fcl&quot; },\r\n                new() { Age = 40, Name = &quot;Mehmet&quot;, Surname = &quot;Sakarya&quot; }\r\n            };\r\n\r\n            return Ok(employees);\r\n        }\r\n\r\n        &#x5B;HttpGet(&quot;&#x5B;action]&quot;)]\r\n        public IActionResult GetCustomers()\r\n        {\r\n            List&lt;Customer&gt; employees = new()\r\n            {\r\n                new() { Age = 30, Name = &quot;Gen\u00e7ay&quot;, Surname = &quot;Y\u0131ld\u0131z&quot; },\r\n                new() { Age = 35, Name = &quot;Mehmet&quot;, Surname = &quot;C\u00fcmb\u00fcl&quot; },\r\n                new() { Age = 40, Name = &quot;Mehmet&quot;, Surname = &quot;Sakarya&quot; }\r\n            };\r\n\r\n            return Ok(employees);\r\n        }\r\n    }\r\n<\/pre>\n<p>G\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere ilgili controller&#8217;da &#8216;GetUsers&#8217;, &#8216;GetEmployees&#8217; ve &#8216;GetCustomers&#8217; olmak \u00fczere \u00fc\u00e7 action metot olu\u015fturulmu\u015ftur. \u015eimdi bu metotlara client \u00fczerinden istek yap\u0131ld\u0131\u011f\u0131nda &#8216;CanWriteResult&#8217; metodundan dolay\u0131 d\u00f6n\u00fc\u015f t\u00fcr\u00fcne g\u00f6re \u00f6zel bi\u00e7imlendirici devreye girecek ve verisel d\u00f6n\u00fc\u015f\u00fcm\u00fc bizim format\u0131m\u0131za g\u00f6re ger\u00e7ekle\u015ftirecektir.<\/p>\n<table>\n<thead>\n<tr>\n<th>User<\/th>\n<th>Employee<\/th>\n<th>Customer<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2021\/09\/Asp.NET-Core-Web-API-Custom-FormattersOzel-Bicimlendiriciler.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2021\/09\/Asp.NET-Core-Web-API-Custom-FormattersOzel-Bicimlendiriciler.png\" alt=\"Asp.NET Core Web API - Custom Formatters(\u00d6zel Bi\u00e7imlendiriciler)\" width=\"323\" height=\"195\" class=\"aligncenter size-full wp-image-23056\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2021\/09\/Asp.NET-Core-Web-API-Custom-FormattersOzel-Bicimlendiriciler.png 323w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2021\/09\/Asp.NET-Core-Web-API-Custom-FormattersOzel-Bicimlendiriciler-300x181.png 300w\" sizes=\"auto, (max-width: 323px) 100vw, 323px\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2021\/09\/Asp.NET-Core-Web-API-Custom-FormattersOzel-Bicimlendiriciler.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2021\/09\/Asp.NET-Core-Web-API-Custom-FormattersOzel-Bicimlendiriciler.png\" alt=\"Asp.NET Core Web API - Custom Formatters(\u00d6zel Bi\u00e7imlendiriciler)\" width=\"323\" height=\"195\" class=\"aligncenter size-full wp-image-23056\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2021\/09\/Asp.NET-Core-Web-API-Custom-FormattersOzel-Bicimlendiriciler.png 323w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2021\/09\/Asp.NET-Core-Web-API-Custom-FormattersOzel-Bicimlendiriciler-300x181.png 300w\" sizes=\"auto, (max-width: 323px) 100vw, 323px\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2021\/09\/Asp.NET-Core-Web-API-Custom-FormattersOzel-Bicimlendiriciler-1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2021\/09\/Asp.NET-Core-Web-API-Custom-FormattersOzel-Bicimlendiriciler-1.png\" alt=\"Asp.NET Core Web API - Custom Formatters(\u00d6zel Bi\u00e7imlendiriciler)\" width=\"316\" height=\"333\" class=\"aligncenter size-full wp-image-23057\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2021\/09\/Asp.NET-Core-Web-API-Custom-FormattersOzel-Bicimlendiriciler-1.png 316w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2021\/09\/Asp.NET-Core-Web-API-Custom-FormattersOzel-Bicimlendiriciler-1-285x300.png 285w\" sizes=\"auto, (max-width: 316px) 100vw, 316px\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Benzer \u015fekilde client&#8217;tan gelecek olan verileri \u00f6zelle\u015ftirilmi\u015f bir \u015fekilde i\u015flememizi sa\u011flayacak olan <code style=\"font-size:13px;\">TextInputFormatter<\/code>&#8216;dan t\u00fcreyecek s\u0131n\u0131f\u0131da geli\u015ftirelim.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class UserInputFormatter : TextInputFormatter\r\n    {\r\n        public UserInputFormatter()\r\n        {\r\n            SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse(&quot;text\/customFormat&quot;));\r\n\r\n            SupportedEncodings.Add(Encoding.UTF8);\r\n            SupportedEncodings.Add(Encoding.Unicode);\r\n        }\r\n        protected override bool CanReadType(Type type)\r\n        {\r\n            return type == typeof(User);\r\n        }\r\n        public override async Task&lt;InputFormatterResult&gt; ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)\r\n        {\r\n            HttpRequest request = context.HttpContext.Request;\r\n            using StreamReader streamReader = new(request.Body, encoding);\r\n            try\r\n            {\r\n                string data = await streamReader.ReadToEndAsync();\r\n                if (!data.StartsWith(&quot;****&quot;))\r\n                {\r\n                    string errorMessage = &quot;Bozuk format! Veri **** ifadesi ile ba\u015flamal\u0131!&quot;;\r\n                    context.ModelState.TryAddModelError(context.ModelName, errorMessage);\r\n                    throw new Exception(errorMessage);\r\n                }\r\n\r\n                string&#x5B;] nameSurname = data.Substring(data.IndexOf(&quot;:&quot;) + 1, data.Length - (data.IndexOf(&quot;:&quot;) + 1)).Trim().Split(&quot; &quot;);\r\n                User user = new()\r\n                {\r\n                    Name = nameSurname&#x5B;0],\r\n                    Surname = nameSurname&#x5B;1]\r\n                };\r\n\r\n                return await InputFormatterResult.SuccessAsync(user);\r\n            }\r\n            catch\r\n            {\r\n                return await InputFormatterResult.FailureAsync();\r\n            }\r\n        }\r\n    }\r\n<\/pre>\n<p>Yukar\u0131daki input formatter s\u0131n\u0131f\u0131n\u0131 incelerseniz e\u011fer yine constructor&#8217;da media t\u00fcr\u00fc ve kodlama bilgileri verilmektedir. &#8216;CanReadType&#8217; metodunda ise client&#8217;\u0131n g\u00f6nderdi\u011fi veriyi kar\u015f\u0131layacak olan metottaki parametre t\u00fcr\u00fc belirlenmektedir. E\u011fer bu parametre t\u00fcr\u00fc &#8216;User&#8217; ise bu bi\u00e7imlendirici gelen datay\u0131 \u00f6zel formata uygun olarak \u00e7\u00f6z\u00fcmleyecek yok e\u011fer de\u011filse varsay\u0131lan olan JSON t\u00fcr\u00fcnden d\u00f6n\u00fc\u015f\u00fcm sa\u011flanacakt\u0131r. &#8216;ReadRequestBodyAsync&#8217; metodunda ise gelen \u00f6zel formattaki datay\u0131 uygun algoritmayla geri \u00e7\u00f6z\u00fcmleyip misal olarak &#8216;User&#8217; t\u00fcr\u00fcne d\u00f6n\u00fc\u015ft\u00fcrmekteyiz. Sizler burada daha farkl\u0131 ve kompleks bir algoritma kullanabilirsiniz. O y\u00fczden farazi bir \u00e7al\u0131\u015fma ger\u00e7ekle\u015ftirilmi\u015ftir. E\u011fer yukar\u0131daki d\u00f6n\u00fc\u015f\u00fcm ba\u015far\u0131yla ger\u00e7ekle\u015ftiriliyorsa bu durum <code style=\"font-size:13px;\">InputFormatterResult.SuccessAsync<\/code> komutuyla bildirilmektedir. Aksi taktirde <code style=\"font-size:13px;\">InputFormatterResult.FailureAsync<\/code> komutu ile s\u00fcre\u00e7te bir hatan\u0131n meydana geldi\u011fi bildirilerek client&#8217;a gerekli d\u00f6n\u00fc\u015f yap\u0131lmaktad\u0131r.<\/p>\n<p>Geli\u015ftirdi\u011fimiz bu \u00f6zel bi\u00e7imlendiriciyi kullanabilmek i\u00e7in yine uygulaman\u0131n &#8216;Startup.cs&#8217; dosyas\u0131nda a\u015fa\u011f\u0131daki bildirinin yap\u0131lmas\u0131 gerekmektedir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class Startup\r\n    {\r\n        .\r\n        .\r\n        .\r\n        public void ConfigureServices(IServiceCollection services)\r\n        {\r\n            .\r\n            .\r\n            .\r\n            services.AddControllers(options =&gt;\r\n            {\r\n                options.OutputFormatters.Insert(0, new UserOutputFormatter());\r\n                options.InputFormatters.Insert(0, new UserInputFormatter());\r\n                options.FormatterMappings.SetMediaTypeMappingForFormat(&quot;customFormat&quot;, MediaTypeHeaderValue.Parse(&quot;text\/customFormat&quot;));\r\n            });\r\n            .\r\n            .\r\n            .\r\n        }\r\n        .\r\n        .\r\n        .\r\n    }\r\n<\/pre>\n<p>Evet&#8230; \u015eimdide deneme ama\u00e7l\u0131 &#8216;UsersController.cs&#8217; isimli controller&#8217;\u0131 a\u015fa\u011f\u0131daki gibi de\u011fi\u015ftirelim ve gerekli testlerimizi ger\u00e7ekle\u015ftirelim.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    &#x5B;Route(&quot;api\/&#x5B;controller]&quot;)]\r\n    &#x5B;ApiController]\r\n    public class UsersController : ControllerBase\r\n    {\r\n        &#x5B;HttpPost]\r\n        public void Post(User model)\r\n        {\r\n\r\n        }\r\n    }\r\n<\/pre>\n<p>Dikkat ederseniz &#8216;Post&#8217; action&#8217;\u0131na client&#8217;tan gelecek olan daha &#8216;User&#8217; t\u00fcr\u00fcndedir. Haliyle bu t\u00fcrdeki t\u00fcm datalar i\u00e7in bizim olu\u015fturdu\u011fumuz input bi\u00e7imlendirici devreye girecektir.<br \/>\n<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2021\/09\/Asp.NET-Core-Web-API-Custom-FormattersOzel-Bicimlendiriciler-2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2021\/09\/Asp.NET-Core-Web-API-Custom-FormattersOzel-Bicimlendiriciler-2.png\" alt=\"Asp.NET Core Web API - Custom Formatters(\u00d6zel Bi\u00e7imlendiriciler)\" width=\"175\" height=\"90\" class=\"aligncenter size-full wp-image-23064\" \/><\/a><a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2021\/09\/Asp.NET-Core-Web-API-Custom-FormattersOzel-Bicimlendiriciler-3.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2021\/09\/Asp.NET-Core-Web-API-Custom-FormattersOzel-Bicimlendiriciler-3.png\" alt=\"Asp.NET Core Web API - Custom Formatters(\u00d6zel Bi\u00e7imlendiriciler)\" width=\"636\" height=\"261\" class=\"aligncenter size-full wp-image-23065\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2021\/09\/Asp.NET-Core-Web-API-Custom-FormattersOzel-Bicimlendiriciler-3.png 636w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2021\/09\/Asp.NET-Core-Web-API-Custom-FormattersOzel-Bicimlendiriciler-3-300x123.png 300w\" sizes=\"auto, (max-width: 636px) 100vw, 636px\" \/><\/a><br \/>\nE\u011fer post edilen data yanl\u0131\u015f bir formatta g\u00f6nderilirse a\u015fa\u011f\u0131daki gibi client uyar\u0131lacakt\u0131r!<br \/>\n<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2021\/09\/Asp.NET-Core-Web-API-Custom-FormattersOzel-Bicimlendiriciler-4.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2021\/09\/Asp.NET-Core-Web-API-Custom-FormattersOzel-Bicimlendiriciler-4.png\" alt=\"Asp.NET Core Web API - Custom Formatters(\u00d6zel Bi\u00e7imlendiriciler)\" width=\"174\" height=\"94\" class=\"aligncenter size-full wp-image-23068\" \/><\/a>Bizim olu\u015fturdu\u011fumuz formata g\u00f6re ba\u015flang\u0131\u00e7 de\u011feri d\u00f6rt adet y\u0131ld\u0131z olmas\u0131 gerekirken burada bir y\u0131ld\u0131z mevcuttur. Haliyle bu tan\u0131ms\u0131z bir format olaca\u011f\u0131ndan dolay\u0131 d\u00f6n\u00fc\u015f\u00fcm s\u00fcrecinde algoritmik bir hatad\u0131r.<\/p>\n<p>Haliyle al\u0131nan sonu\u00e7;<br \/>\n<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2021\/09\/Asp.NET-Core-Web-API-Custom-FormattersOzel-Bicimlendiriciler-5.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2021\/09\/Asp.NET-Core-Web-API-Custom-FormattersOzel-Bicimlendiriciler-5.png\" alt=\"Asp.NET Core Web API - Custom Formatters(\u00d6zel Bi\u00e7imlendiriciler)\" width=\"570\" height=\"258\" class=\"aligncenter size-full wp-image-23069\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2021\/09\/Asp.NET-Core-Web-API-Custom-FormattersOzel-Bicimlendiriciler-5.png 570w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2021\/09\/Asp.NET-Core-Web-API-Custom-FormattersOzel-Bicimlendiriciler-5-300x136.png 300w\" sizes=\"auto, (max-width: 570px) 100vw, 570px\" \/><\/a>\u015feklinde olacakt\u0131r.<\/p>\n<p>Nihai olarak;<br \/>\nBu makalede Asp.NET Core&#8217;un JSON yahut XML gibi veri formatlar\u0131na istinaden kendimize ait \u00f6zelle\u015ftirilmi\u015f bi\u00e7imlendiricilerimizi olu\u015fturmam\u0131z\u0131 sa\u011flayan ve b\u00f6ylece \u00f6zelle\u015ftirilmi\u015f formatlara sahip \u00fc\u00e7\u00fcnc\u00fc taraf sistemlerle uygulama baz\u0131nda rahatl\u0131kla ileti\u015fim kurabilmemize imkan tan\u0131yan harika bir mimari oldu\u011funu ve ihtiya\u00e7 do\u011frultusunda \u00f6zel bi\u00e7imlendiricilerin mimarisel a\u00e7\u0131dan bi\u00e7ilmi\u015f kaftan oldu\u011funu g\u00f6rm\u00fc\u015f olduk.<\/p>\n<p>\u0130lgilenenlerin faydalanmas\u0131 dile\u011fiyle&#8230;<br \/>\nSonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#8230;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Asp.NET Core Web API &#8211; Custom Formatters(\u00d6zel Bi\u00e7imlendiriciler)<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":3223,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2739,3102],"tags":[2679,4399,4400,4402,4401],"class_list":["post-23014","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-net-core","category-asp-net-core","tag-asp-net-core","tag-asp-net-core-custom-formatters","tag-asp-net-core-ozel-bicimlendiriciler","tag-asp-net-core-web-api-custom-formatters","tag-custom-formatters"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/23014","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=23014"}],"version-history":[{"count":52,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/23014\/revisions"}],"predecessor-version":[{"id":23073,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/23014\/revisions\/23073"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/3223"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=23014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=23014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=23014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}