﻿
{"id":9716,"date":"2018-10-02T21:54:00","date_gmt":"2018-10-02T21:54:00","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=9716"},"modified":"2018-10-02T21:54:00","modified_gmt":"2018-10-02T21:54:00","slug":"asp-net-core-2-mvcde-custom-routing-constraint-olusturma","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/asp-net-core-2-mvcde-custom-routing-constraint-olusturma\/","title":{"rendered":"Asp.NET Core 2 MVC&#8217;de Custom Routing Constraint Olu\u015fturma"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Bu i\u00e7eri\u011fimizde Asp.NET Core 2 MVC&#8217;de \u00f6zel routing k\u0131s\u0131tlamalar\u0131 olan &#8220;Custom Routing Constraint&#8221; yap\u0131lar\u0131n\u0131n nas\u0131l in\u015fa edildi\u011fini \u00f6\u011frenece\u011fiz. Custom routing constraint yap\u0131lar\u0131n\u0131 in\u015fa ederken, gelen bir iste\u011fin kulland\u0131\u011f\u0131 parametrenin de\u011ferini filtreleyerek, ceyran eden i\u015fleyi\u015fe m\u00fcdahalede bulunacak ve bekledi\u011fimiz \u015fartlara uygun parametre de\u011ferleri e\u015fli\u011finde bir talep s\u00f6z konusu de\u011filse ilgili route yap\u0131s\u0131 tetiklenmeyecek ve korumaya al\u0131nacakt\u0131r.<\/p>\n<h4>Route K\u0131s\u0131tlamalar\u0131n\u0131 Kullanaca\u011f\u0131m\u0131z Durumlar<\/h4>\n<p>Custom routing constraint yap\u0131lar\u0131n\u0131 a\u015fa\u011f\u0131daki durumlarda kullanmay\u0131 tercih edebiliriz;<\/p>\n<ul>\n<li>Requestleri \u00f6zel parametre de\u011ferleriyle kar\u015f\u0131lamak istedi\u011fimizde,<\/li>\n<li>Alt ya da \u00fcst limitli say\u0131sal verilerle \u00e7al\u0131\u015fmak istedi\u011fimizde,<\/li>\n<li>Sadece metinsel, say\u0131sal ya da mant\u0131ksal tipte de\u011ferlerden herhangi birinin parametre de\u011feri olarak belirtilmesini istedi\u011fimizde<\/li>\n<li>vs. vs.<\/li>\n<\/ul>\n<p>route k\u0131s\u0131tlamalar\u0131n\u0131 kullanabiliriz.<\/p>\n<h4>IRouteConstraint Interface&#8217;i<\/h4>\n<p>\u00d6zel route k\u0131s\u0131tlamalar\u0131n\u0131 olu\u015fturabilmek i\u00e7in IRouteConstraint interface&#8217;ini kullanmaktay\u0131z. Bu interface, uyguland\u0131\u011f\u0131 s\u0131n\u0131fa &#8220;Match&#8221; isminde bir metot implement etmektedir. Bu metot i\u00e7erisinde belirtece\u011fimiz kriterler ile requestten gelen parametre de\u011ferlerini e\u015fle\u015ftirecek ve bu e\u015fle\u015ftirme neticesinde geriye true ya da false de\u011feri d\u00f6necektir. E\u011fer true de\u011feri d\u00f6nerse tetiklenen route ilgili parametre de\u011feri ile tetiklenecektir. Yok e\u011fer false de\u011feri gelirse 404 hatas\u0131 verilecektir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public interface IRouteConstraint\r\n    {\r\n        bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection);\r\n    }\r\n<\/pre>\n<p>&#8220;Match&#8221; metodunun parametrelerini incelersek e\u011fer;<\/p>\n<ul>\n<li><strong><em>HttpContext httpContext<\/em><\/strong><br \/>\nYap\u0131lan request ile birlikte response, session vs. gibi t\u00fcm http durumlar\u0131yla ilgili gerekli bilgileri i\u00e7erir.\n<\/li>\n<li><strong><em>IRouter route<\/em><\/strong><br \/>\nK\u0131s\u0131tlamalara ait y\u00f6nlendiricidir.\n<\/li>\n<li><strong><em>string routeKey<\/em><\/strong><br \/>\nK\u0131s\u0131tlay\u0131c\u0131n\u0131n ba\u011fland\u0131\u011f\u0131 parametrenin ad\u0131n\u0131 tutmaktad\u0131r.\n<\/li>\n<li><strong><em>RouteValueDictionary values<\/em><\/strong><br \/>\nUrl i\u00e7erisindeki t\u00fcm parametreleri tutmaktad\u0131r.\n<\/li>\n<li><strong><em>RouteDirection routeDirection<\/em><\/strong><br \/>\nRequest neticesinde bir url&#8217;in i\u015fleniyor mu? olu\u015fturuluyor mu? bilgisini verir. &#8220;IncomingRequest&#8221; ve &#8220;UrlGeneration&#8221; olmak \u00fczere iki adet de\u011fere sahiptir.<\/p>\n<ul>\n<li><em>IncomingRequest<\/em><br \/>\nUrl&#8217;in i\u015flendi\u011fine dair bilgi verir.\n<\/li>\n<li><em>UrlGeneration<\/em><br \/>\nUrl&#8217;in olu\u015fturuldu\u011funa dair bilgi verir.\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>Custom Routing Constraint Olu\u015fturma ve Kullanma<\/h4>\n<p>Custom routing constraint olu\u015fturmak i\u00e7in a\u015fa\u011f\u0131daki \u00f6rne\u011fi inceleyiniz;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class WeebDayConstraint : IRouteConstraint\r\n    {\r\n        string&#x5B;] _days = { &quot;pazartesi&quot;, &quot;sali&quot;, &quot;carsamba&quot;, &quot;persembe&quot;, &quot;cuma&quot;, &quot;cumartesi&quot;, &quot;pazar&quot; };\r\n        public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)\r\n         =&gt; _days.Contains(values&#x5B;routeKey]?.ToString().ToLower());\r\n    }\r\n<\/pre>\n<p>Yukar\u0131daki \u00f6rnek route constraint kodlar\u0131n\u0131 incelerseniz e\u011fer &#8220;string&#8221; tipindeki &#8220;_days&#8221; isimli dizide bulunan de\u011ferlere e\u015f de\u011fer olan parametre de\u011ferleri durumunda request \u00e7al\u0131\u015ft\u0131r\u0131lacak ve ilgili route tetiklenecektir.<\/p>\n<p>Custom routing constraint yap\u0131m\u0131z\u0131 olu\u015fturduktan sonra s\u0131ra route yap\u0131lanmas\u0131nda kullanmak istedi\u011fimiz route \u015femalar\u0131ndaki parametrelere ilgili constrainti a\u015fa\u011f\u0131daki gibi tan\u0131mlamaya gelmi\u015f bulunmaktad\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\n.\r\n        public void Configure(IApplicationBuilder app, IHostingEnvironment env)\r\n        {\r\n            app.UseMvc(routes =&gt;\r\n            {\r\n                routes.MapRoute(\r\n                    name: &quot;Default&quot;,\r\n                    template: &quot;{controller=Home}\/{action=Index}\/{day?}&quot;,\r\n                    constraints: new { day = new WeebDayConstraint() },\r\n                    defaults: new { }\r\n                        );\r\n            });\r\n        }\r\n.\r\n.\r\n.\r\n<\/pre>\n<p>Yukar\u0131daki \u00f6rnek kod blo\u011funda g\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi &#8220;Default&#8221; route&#8217;unun &#8220;day&#8221; parametresine olu\u015fturdu\u011fumuz custom constraint tan\u0131mlanm\u0131\u015f bulunmaktad\u0131r.<\/p>\n<p>Ayr\u0131ca bir ba\u015fka tan\u0131mlama y\u00f6ntemi olarak a\u015fa\u011f\u0131daki ayarlamalar\u0131 ger\u00e7ekle\u015ftirebiliriz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class Startup\r\n    {\r\n        public void ConfigureServices(IServiceCollection services)\r\n        {\r\n            #region Route Custom Constraint\r\n            services.Configure&lt;RouteOptions&gt;(options =&gt; options.ConstraintMap.Add(&quot;weekday&quot;, typeof(WeebDayConstraint)));\r\n            #endregion\r\n\r\n            services.AddMvc();\r\n        }\r\n\r\n        public void Configure(IApplicationBuilder app, IHostingEnvironment env)\r\n        {\r\n            app.UseMvc(routes =&gt;\r\n            {\r\n                routes.MapRoute(\r\n                    name: &quot;Default&quot;,\r\n                    template: &quot;{controller=Home}\/{action=Index}\/{day:weekday?}&quot;\r\n                        );\r\n            });\r\n        }\r\n    }\r\n<\/pre>\n<p>G\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere &#8220;ConfigureServices&#8221; metodunda olu\u015fturmu\u015f oldu\u011fumuz custom constraint s\u0131n\u0131f\u0131m\u0131z\u0131 &#8220;weekday&#8221; etiketiyle &#8220;RouteOptions&#8221; nesnesi arac\u0131l\u0131\u011f\u0131yla ayarlamakta ve &#8220;Configure&#8221; metodu i\u00e7erisindeki route yap\u0131lanmas\u0131nda kullan\u0131laca\u011f\u0131 parametrede &#8220;day:weekday&#8221; \u015feklinde kullanabilmekteyiz.<\/p>\n<p>Bu tan\u0131mlamalardan sonra &#8220;day&#8221; parametresine custom routing constraint olarak tasarlad\u0131\u011f\u0131m\u0131z &#8220;WeebDayConstraint&#8221; isimli class\u0131m\u0131zda belirtti\u011fimiz g\u00fcn de\u011ferleri d\u0131\u015f\u0131nda bir de\u011fer verildi\u011fi taktirde ilgili route tetiklenmeyecek ve kullan\u0131c\u0131ya 404 hatas\u0131 g\u00f6nderilecektir.<\/p>\n<p>Dolay\u0131s\u0131yla bizler route \u015femalar\u0131ndaki parametrelerin alabilece\u011fi de\u011ferleri \u00f6zel k\u0131s\u0131tlamalar olu\u015fturarak filtreleyebilmekte ve route \u015femalar\u0131m\u0131z\u0131n i\u015flevsel g\u00fcvenli\u011fini bu \u015fekilde sa\u011flayabilmekteyiz.<\/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&#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>Merhaba, Bu i\u00e7eri\u011fimizde Asp.NET Core 2 MVC&#8217;de \u00f6zel routing k\u0131s\u0131tlamalar\u0131 olan &#8220;Custom Routing Constraint&#8221; yap\u0131lar\u0131n\u0131n nas\u0131l in\u015fa edildi\u011fini \u00f6\u011frenece\u011fiz. Custom routing constraint yap\u0131lar\u0131n\u0131 in\u015fa ederken, gelen bir iste\u011fin kulland\u0131\u011f\u0131 parametrenin de\u011ferini filtreleyerek, ceyran eden&#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":9596,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2668,2656],"tags":[2651,2678,2677],"class_list":["post-9716","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-2-mvc","tag-custom-routing-constraint","tag-irouteconstraint"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/9716","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=9716"}],"version-history":[{"count":15,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/9716\/revisions"}],"predecessor-version":[{"id":9732,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/9716\/revisions\/9732"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/9596"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=9716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=9716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=9716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}