﻿
{"id":10271,"date":"2019-02-04T20:14:10","date_gmt":"2019-02-04T20:14:10","guid":{"rendered":"https:\/\/www.gencayyildiz.com\/blog\/?p=10271"},"modified":"2019-02-04T20:16:36","modified_gmt":"2019-02-04T20:16:36","slug":"asp-net-coreda-route-handler-olusturma","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/asp-net-coreda-route-handler-olusturma\/","title":{"rendered":"Asp.NET Core&#8217;da Route Handler Olu\u015fturma"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Asp.NET Core uygulamalar\u0131nda kendi route handler yap\u0131lanmam\u0131z\u0131 olu\u015fturarak belli ba\u015fl\u0131 isteklere \u00f6zel cevaplar verebiliriz. Asl\u0131nda bizler bu konuya dair \u00f6nceden <a href=\"https:\/\/www.gencayyildiz.com\/blog\/asp-net-mvcde-httphandler-olusturma\/\" target=\"_blank\" rel=\"noopener\">Asp.NET MVC\u2019de HttpHandler Olu\u015fturma<\/a> ba\u015fl\u0131\u011f\u0131 alt\u0131nda bir de\u011ferlendirmeyi standart Asp.NET MVC i\u00e7in kaleme alm\u0131\u015f bulunmaktay\u0131z. Bu i\u00e7eri\u011fimizde ise benzer yap\u0131lanmay\u0131 Asp.NET Core uygulamas\u0131 i\u00e7in nas\u0131l ger\u00e7ekle\u015ftirece\u011fimizi; konuyu daha geni\u015f de\u011ferlendirerek, Core \u00e7ekirde\u011fi \u00e7er\u00e7evesinde daha fazla muhakeme ederek inceleyece\u011fiz.<\/p>\n<p>\u0130lk olarak Asp.NET Core mimarisinde bir request s\u00fcrecinde hangi ad\u0131mlar\u0131n ger\u00e7ekle\u015fti\u011fini hat\u0131rlayarak, ilgili konumuzun kuramsal temelini olu\u015ftural\u0131m. Ard\u0131ndan route handler yap\u0131lanmas\u0131n\u0131n hangi kurallar \u00e7er\u00e7evesinde, nas\u0131l sa\u011flanaca\u011f\u0131n\u0131 inceleyerek bir image boyutland\u0131rma kurgusu \u00fczerinden \u00f6rneklendirme yap\u0131p makalemizi noktalayal\u0131m.<\/p>\n<p><a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/02\/Asp.NET-Coreda-Route-Handler-Olu\u015fturma.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-10274\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/02\/Asp.NET-Coreda-Route-Handler-Olu\u015fturma.png\" alt=\"Asp.NET Core'da Route Handler Olu\u015fturma\" width=\"277\" height=\"307\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/02\/Asp.NET-Coreda-Route-Handler-Olu\u015fturma.png 277w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/02\/Asp.NET-Coreda-Route-Handler-Olu\u015fturma-271x300.png 271w\" sizes=\"auto, (max-width: 277px) 100vw, 277px\" \/><\/a><\/p>\n<p><strong>Request s\u00fcrecinde neler oluyor?<\/strong><\/p>\n<ul>\n<li>Web sunucusuna istek gelir,<\/li>\n<li>Varsa <a href=\"https:\/\/www.gencayyildiz.com\/blog\/asp-net-core-2de-middleware-yapisi-ve-kullanimi\/\" target=\"_blank\" rel=\"noopener\">middleware<\/a>&#8216;ler devreye girer, s\u0131ras\u0131yla tek tek i\u015flenir,<\/li>\n<li>Taa ki s\u0131ra route middlewareimiz olan UseMvcWithDefaultRoute yahut UseMvc middlewarelerine gelir. Bir ba\u015fka deyi\u015fle y\u00f6nlendirme katman\u0131 tetiklenirde diyebiliriz.<\/li>\n<li>Y\u00f6nlendirme katman\u0131, gelen iste\u011fi \u00f6nceden tan\u0131mlanm\u0131\u015f yahut varsay\u0131lan olarak belirtilmi\u015f \u015femalarla e\u015fle\u015fip e\u015fle\u015fmedi\u011fini inceleyecek ve duruma g\u00f6re y\u00f6nlendirmede bulunacakt\u0131r.<\/li>\n<\/ul>\n<p>s\u00fcre\u00e7teki genel olaylar bu \u00e7er\u00e7evede ceyran etmektedir.<\/p>\n<p>\u0130\u015fte bu i\u015fleyi\u015fin son basama\u011f\u0131nda, s\u00fcre\u00e7te en son route handler dedi\u011fimiz noktaya temas edilecektir. Burada e\u015fle\u015fen \u015femalara kar\u015f\u0131 route mekanizmalar\u0131 handler edilerek gerekli y\u00f6nlendirmeler response edilecektir.<\/p>\n<p>Bizler klasik route \u015femalar\u0131 olu\u015fturmak yerine custom route handler yap\u0131lar\u0131 in\u015fa ederek bu son noktada devreye sokabiliriz. Dolay\u0131s\u0131yla kendimize \u00f6zel istekler tan\u0131mlayabilir ve farkl\u0131 i\u015flemlere tabi tutulan y\u00f6nlendirmelerde bulunabiliriz. T\u00fcm bu yap\u0131lanmay\u0131 custom route handler olu\u015fturarak ger\u00e7ekle\u015ftirebiliriz ve bunu yaparken Asp.NET Core mimarisinde, standart Asp.NET MVC mimarisindeki yapt\u0131\u011f\u0131m\u0131z \u00e7al\u0131\u015fmadan olduk\u00e7a farkl\u0131 ve sade bir \u015fekilde \u00e7aba g\u00f6stererek sa\u011flayabiliriz.<\/p>\n<p><strong>Peki custom route handler nas\u0131l olu\u015fturulur?<\/strong><br \/>\nBunun i\u00e7in tek yap\u0131lmas\u0131 gereken &#8220;RequestDelegate&#8221; isimli delegate&#8217;i geriye d\u00f6nen metot tasarlamaktad\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class ExampleHandler\r\n    {\r\n        public RequestDelegate Handler()\r\n        {\r\n            return async c =&gt; await Task.Run(async () =&gt;\r\n            {\r\n                await c.Response.WriteAsync(&quot;hello world&quot;);\r\n            });\r\n        }\r\n    }\r\n<\/pre>\n<p>\u0130\u015fte bu kadar&#8230; Ne kadar basit de\u011fil mi? Ekrana &#8220;hello world&#8221; yazacak custom route handler&#8217;\u0131m\u0131z\u0131 olu\u015fturmu\u015f olduk. \u015eimdi bu olu\u015fturmu\u015f oldu\u011fumuz route handler&#8217;\u0131 kullanabilmek i\u00e7in route \u015femalar\u0131nda a\u015fa\u011f\u0131daki gibi belirtmemiz yeterli olacakt\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class Startup\r\n    {\r\n        IHostingEnvironment _hostingEnvironment;\r\n        public Startup(IHostingEnvironment hostingEnvironment) =&gt; _hostingEnvironment = hostingEnvironment;\r\n\r\n        public void ConfigureServices(IServiceCollection services)\r\n        {\r\n            services.AddMvc();\r\n        }\r\n\r\n        public void Configure(IApplicationBuilder app, IHostingEnvironment env)\r\n        {\r\n            if (env.IsDevelopment())\r\n                app.UseDeveloperExceptionPage();\r\n\r\n            app.UseStaticFiles();\r\n            app.UseMvcWithDefaultRoute();\r\n            app.UseMvc(routes =&gt;\r\n            {\r\n                routes.MapRoute(&quot;customroute&quot;, new ExampleHandler().Handler());\r\n                routes.MapRoute(\r\n                    name: &quot;Default&quot;,\r\n                    template: &quot;{controller=Home}\/{action=Index}\/{id?}&quot;\r\n                    );\r\n            });\r\n\r\n        }\r\n    }\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funu incelerseniz e\u011fer olu\u015fturdu\u011fumuz custom route handler&#8217;\u0131 nerede kulland\u0131\u011f\u0131m\u0131z\u0131 belki direkt olarak yakalayamayabilirsiniz. O y\u00fczden daha net odaklanabilmek i\u00e7in ilgili sat\u0131rlar\u0131 sade bir \u015fekilde a\u015fa\u011f\u0131ya al\u0131yorum&#8230;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\n.\r\n            app.UseMvc(routes =&gt;\r\n            {\r\n                routes.MapRoute(&quot;customroute&quot;, new ExampleHandler().Handler());\r\n                routes.MapRoute(\r\n                    name: &quot;Default&quot;,\r\n                    template: &quot;{controller=Home}\/{action=Index}\/{id?}&quot;\r\n                    );\r\n            });\r\n.\r\n.\r\n.\r\n<\/pre>\n<p>Evet&#8230; G\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere tan\u0131mlad\u0131\u011f\u0131m\u0131z route handler&#8217;\u0131m\u0131z, &#8220;customroute&#8221; \u015feklinde tan\u0131mlanm\u0131\u015f bir route \u00fczerinde handler edilmektedir.<br \/>\n<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/02\/Asp.NET-Coreda-Route-Handler-Olu\u015fturma.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-10292\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/02\/Asp.NET-Coreda-Route-Handler-Olu\u015fturma.gif\" alt=\"Asp.NET Core'da Route Handler Olu\u015fturma\" width=\"504\" height=\"148\" \/><\/a><\/p>\n<p>\u0130\u015fte bu kadar basit&#8230;<\/p>\n<p>\u015eimdi bu temelde image boyutland\u0131rma \u00fczerine \u00f6rneklendirme ile konumuzu zenginle\u015ftirip, makalemizi noktalayabiliriz.<\/p>\n<h4>Custom Route Handler \u0130le Image Boyutland\u0131rma<\/h4>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class ImageHandler\r\n    {\r\n        IHostingEnvironment _hostingEnvironment;\r\n        public ImageHandler(IHostingEnvironment hostingEnvironment) =&gt; _hostingEnvironment = hostingEnvironment;\r\n        public RequestDelegate Handler()\r\n        {\r\n            return async c =&gt; await Task.Run(async () =&gt;\r\n            {\r\n                string fileName = c.GetRouteValue(&quot;fileName&quot;).ToString();\r\n\r\n                FileInfo imageFile = new FileInfo($&quot;{_hostingEnvironment.WebRootPath}\\\\{fileName}&quot;);\r\n\r\n                using (MagickImage magicImage = new MagickImage(imageFile))\r\n                {\r\n                    int width = magicImage.Width, height = magicImage.Height;\r\n\r\n                    if (!string.IsNullOrEmpty(c.Request.Query&#x5B;&quot;w&quot;].ToString()))\r\n                        width = int.Parse(c.Request.Query&#x5B;&quot;w&quot;].ToString());\r\n                    if (!string.IsNullOrEmpty(c.Request.Query&#x5B;&quot;h&quot;].ToString()))\r\n                        height = int.Parse(c.Request.Query&#x5B;&quot;h&quot;]);\r\n\r\n\r\n                    magicImage.Resize(width, height);\r\n                    var buffer = magicImage.ToByteArray();\r\n                    c.Response.Clear();\r\n                    c.Response.ContentType = string.Concat(&quot;image\/&quot;, imageFile.Extension.Replace(&quot;.&quot;, &quot;&quot;));\r\n                    await c.Response.Body.WriteAsync(buffer, 0, buffer.Length);\r\n                }\r\n\r\n                await c.Response.WriteAsync($&quot;{_hostingEnvironment.WebRootPath}\\\\{fileName}&quot;);\r\n            });\r\n        }\r\n    }\r\n<\/pre>\n<p>Yukar\u0131daki &#8220;ImageHandler&#8221; isimli custom route handler&#8217;\u0131 incelerseniz e\u011fer tetiklendi\u011fi \u00f6zel y\u00f6nlendirici \u00fczerinde gelen baz\u0131 route ve querystring parametreleri e\u015fli\u011finde g\u00f6rseli boyutland\u0131rmakta ve ContentType de\u011ferini gelen g\u00f6rselin extension\u0131na uygun bir \u015fekilde ayarlayarak response etmektedir. Ayr\u0131ca resim dosyalar\u0131 \u00fczerinde i\u015flem yapabilmek i\u00e7inde MagicImage k\u00fct\u00fcphanesi kullan\u0131lmaktad\u0131r.<\/p>\n<p>\u0130lgili custom route handler&#8217;a \u00f6zel olarak a\u015fa\u011f\u0131daki gibi bir route tan\u0131mlayal\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class Startup\r\n    {\r\n        IHostingEnvironment _hostingEnvironment;\r\n        public Startup(IHostingEnvironment hostingEnvironment) =&gt; _hostingEnvironment = hostingEnvironment;\r\n\r\n        public void ConfigureServices(IServiceCollection services)\r\n        {\r\n            services.AddMvc();\r\n        }\r\n\r\n        public void Configure(IApplicationBuilder app, IHostingEnvironment env)\r\n        {\r\n            if (env.IsDevelopment())\r\n                app.UseDeveloperExceptionPage();\r\n\r\n            app.UseStaticFiles();\r\n            app.UseMvcWithDefaultRoute();\r\n            app.UseMvc(routes =&gt;\r\n            {\r\n                routes.MapRoute(&quot;image\/{fileName}&quot;, new ImageHandler(_hostingEnvironment).Handler());\r\n                routes.MapRoute(\r\n                    name: &quot;Default&quot;,\r\n                    template: &quot;{controller=Home}\/{action=Index}\/{id?}&quot;\r\n                    );\r\n            });\r\n        }\r\n    }\r\n<\/pre>\n<p>Bu i\u015flemden sonra a\u015fa\u011f\u0131daki ekran g\u00f6r\u00fcnt\u00fcs\u00fcnde g\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere ilgili route image ContentType&#8217;\u0131nda response edilmektedir.<br \/>\n<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/02\/Asp.NET-Coreda-Route-Handler-Olu\u015fturma-1.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-10299\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/02\/Asp.NET-Coreda-Route-Handler-Olu\u015fturma-1.gif\" alt=\"Asp.NET Core'da Route Handler Olu\u015fturma\" width=\"800\" height=\"580\" \/><\/a><\/p>\n<p>Ayr\u0131ca bu \u015fekilde handler edilen image nesneleri &lt;img src=&#8221;&#8230;&#8221;\u00a0 \/&gt; html nesnesinde direkt olarak kaynak olarak belirtilebilir ve dolay\u0131s\u0131yla dinamik bir \u015fekilde resim yap\u0131lanmas\u0131da kullan\u0131labilir \u015fekilde ayarlanm\u0131\u015f olur.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;img src=&quot;https:\/\/localhost:5001\/image\/image-1.jpg?w=200&amp;h=250&quot; \/&gt;\r\n&lt;img src=&quot;https:\/\/localhost:5001\/image\/image-2.jpg?w=300&amp;h=350&quot; \/&gt;\r\n&lt;img src=&quot;https:\/\/localhost:5001\/image\/image-3.jpg?w=400&amp;h=450&quot; \/&gt;\r\n<\/pre>\n<p><a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/02\/Asp.NET-Coreda-Route-Handler-Olu\u015fturma-1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/02\/Asp.NET-Coreda-Route-Handler-Olu\u015fturma-1.png\" alt=\"Asp.NET Core&#039;da Route Handler Olu\u015fturma\" width=\"920\" height=\"354\" class=\"aligncenter size-full wp-image-10302\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/02\/Asp.NET-Coreda-Route-Handler-Olu\u015fturma-1.png 920w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/02\/Asp.NET-Coreda-Route-Handler-Olu\u015fturma-1-300x115.png 300w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/02\/Asp.NET-Coreda-Route-Handler-Olu\u015fturma-1-768x296.png 768w\" sizes=\"auto, (max-width: 920px) 100vw, 920px\" \/><\/a><br \/>\nG\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere&#8230;<\/p>\n<p>\u0130lgilenenlerin faydalanmas\u0131 dile\u011fiyle&#8230;<\/p>\n<p>Sonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar dilerim.<\/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>Merhaba, Asp.NET Core uygulamalar\u0131nda kendi route handler yap\u0131lanmam\u0131z\u0131 olu\u015fturarak belli ba\u015fl\u0131 isteklere \u00f6zel cevaplar verebiliriz. Asl\u0131nda bizler bu konuya dair \u00f6nceden Asp.NET MVC\u2019de HttpHandler Olu\u015fturma ba\u015fl\u0131\u011f\u0131 alt\u0131nda bir de\u011ferlendirmeyi standart Asp.NET MVC i\u00e7in kaleme&#46;&#46;&#46;<!-- 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":10242,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2668,2656],"tags":[2679,2755,2752,2751,2754,2753],"class_list":["post-10271","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-asp-net-core-2","category-asp-net-core-2-mvc","tag-asp-net-core","tag-asp-net-core-route-handler","tag-magicimage","tag-requestdelegate","tag-route-handler","tag-route-handler-olusturma"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/10271","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=10271"}],"version-history":[{"count":28,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/10271\/revisions"}],"predecessor-version":[{"id":10303,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/10271\/revisions\/10303"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/10242"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=10271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=10271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=10271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}