﻿
{"id":5772,"date":"2017-03-19T02:11:20","date_gmt":"2017-03-19T02:11:20","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=5772"},"modified":"2017-03-19T02:11:20","modified_gmt":"2017-03-19T02:11:20","slug":"asp-net-mvcde-nonaction-ve-childactiononly-attributelari","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/asp-net-mvcde-nonaction-ve-childactiononly-attributelari\/","title":{"rendered":"Asp.NET MVC&#8217;de NonAction ve ChildActionOnly Attributelar\u0131"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Biliyorsunuz ki, Asp.NET MVC mimarisinde Controller katman\u0131nda bulunan Action metodlar\u0131 taray\u0131c\u0131m\u0131z\u0131n adres \u00e7ubu\u011fu \u00fczerinden tetikleyebilmekteyiz. Buradan \u00e7\u0131karabilece\u011fimiz sonu\u00e7, server i\u015flemleri dedi\u011fimiz k\u0131sma kullan\u0131c\u0131 isterse adres \u00fczerinden m\u00fcdahale edebilmekte ve bu m\u00fcdahale kritik noktalarda ehemmiyetli olabilmektedir. \u0130\u015fte bu tarz durumlara kar\u015f\u0131 g\u00fcvenlik \u00f6nlemi maksatl\u0131 kritik \u00e7al\u0131\u015fmalar\u0131n oldu\u011fu Action metodlar\u0131na <em>NonAction<\/em> yahut <em>ChildActionOnly<\/em> attributelar\u0131 ile adresten eri\u015fimi engelleyebiliriz.<\/p>\n<p>\u00d6ncelikle NonAction attributeunu ele alarak ba\u015flayal\u0131m.<\/p>\n<h3>NonAction Attribute&#8217;u<\/h3>\n<p>NonAction metodu, Controller i\u00e7erisinde public olarak tan\u0131mlanm\u0131\u015f normal metodlar\u0131 temsil etmektedir. Daha do\u011fru bir \u015fekilde ifade etmemiz gerekirse e\u011fer, ilgili metod bir Action metod \u00f6zelli\u011finde kullan\u0131lmayacaksa o metodu NonAction attribute&#8217;u ile i\u015faretleyerek bir Action metod olmad\u0131\u011f\u0131n\u0131 bildiririz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class HomeController : Controller\r\n    {\r\n        public int Islem()\r\n        {\r\n            return 4 + 2;\r\n        }\r\n    }\r\n<\/pre>\n<p>Yukar\u0131da g\u00f6rm\u00fc\u015f oldu\u011funuz Islem metodu geriye int tipinde bir de\u011fer d\u00f6nd\u00fcren bir Action metotdur. Haliyle &#8220;\/Home\/Islem&#8221; adresine taray\u0131c\u0131m\u0131z\u0131n adres \u00e7ubu\u011fundan request(talep) g\u00f6nderdi\u011fimiz zaman bize result(sonu\u00e7) olarak yapt\u0131\u011f\u0131 i\u015flemin de\u011ferini d\u00f6nd\u00fcrecektir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        &#x5B;NonAction]\r\n        public int Islem()\r\n        {\r\n            return 4 + 2;\r\n        }\r\n<\/pre>\n<p>E\u011fer ki ekranda da g\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi ilgili metod NonAction attribute&#8217;u ile i\u015faretlenirse &#8220;\/Home\/Islem&#8221; adresi bir Action metodu i\u015faret etmeyece\u011finden dolay\u0131 \u00e7al\u0131\u015fmayacakt\u0131r.<\/p>\n<p>Asl\u0131nda NonAction attributeu ilgili metodu birnevi private durumundaki gibi d\u0131\u015far\u0131dan eri\u015fimsiz bir hale getirmektedir.<\/p>\n<p>\u015eimdi gelelim ChildActionOnly attributeuna.<\/p>\n<h3>ChildActionOnly Attribute&#8217;u<\/h3>\n<p>ChildActionOnly attributeu, bir Controller&#8217;da bulunan Action metodunun direkt olarak urlden tetiklenmesini engellemektedir.<br \/>\nBurada dikkat etmenizi istedi\u011fim nokta \u015fudur ki;<\/p>\n<blockquote><p>\nNonAction attributeu Action metod olmayan bir metodun urlden tetiklenmesini engellerken, ChildActionOnly attributeu ise Action metod olan bir metodun urlden eri\u015filmesini engellemektedir. \u0130kisi aras\u0131nda ki temel fark budur.<\/p><\/blockquote>\n<p>\u015eimdi a\u015fa\u011f\u0131daki kod blo\u011funu inceleyiniz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class HomeController : Controller\r\n    {\r\n        &#x5B;ChildActionOnly]\r\n        public int Islem()\r\n        {\r\n            return 4 + 2;\r\n        }\r\n    }\r\n<\/pre>\n<p>Kod blo\u011funda g\u00f6rm\u00fc\u015f oldu\u011funuz Islem metodu bir Action metotdur. E\u011fer ki bu metodu NonAction attributeu ile i\u015faretlemi\u015f olsayd\u0131k bu s\u0131radan bir metod(Action olmayan metod) olacakt\u0131. Lakin ChildActionOnly attributeu ile i\u015faretlendi\u011finden dolay\u0131 url \u00fczerinden direkt eri\u015filemeyen bir Action metod olmu\u015ftur. O y\u00fczden ilgili attributeumuzun isminde &#8220;Child&#8221; ve &#8220;Only&#8221; kelimesi ge\u00e7mektedir. Yani &#8220;Sadece Evlat Action&#8221; anlam\u0131nda bir s\u0131fata sahiptir.<\/p>\n<p><a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/03\/Asp.NET-MVCde-NonAction-ve-ChildActionOnly-Attributelar\u0131.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/03\/Asp.NET-MVCde-NonAction-ve-ChildActionOnly-Attributelar\u0131.png\" alt=\"Asp.NET MVC&#039;de NonAction ve ChildActionOnly Attributelar\u0131\" width=\"902\" height=\"240\" class=\"aligncenter size-full wp-image-5781\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/03\/Asp.NET-MVCde-NonAction-ve-ChildActionOnly-Attributelar\u0131.png 902w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/03\/Asp.NET-MVCde-NonAction-ve-ChildActionOnly-Attributelar\u0131-300x80.png 300w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/03\/Asp.NET-MVCde-NonAction-ve-ChildActionOnly-Attributelar\u0131-768x204.png 768w\" sizes=\"auto, (max-width: 902px) 100vw, 902px\" \/><\/a><\/p>\n<p>G\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere ilgili Action urlden tetiklendi\u011fi vakit yukar\u0131daki ekran g\u00f6r\u00fcnt\u00fcs\u00fcnde ki hatayla kar\u015f\u0131la\u015f\u0131lmaktad\u0131r.<\/p>\n<h5>Peki ChildActionOnly Attribute&#8217;u \u0130le \u0130\u015faretlenmi\u015f Bir Action Metodu Nas\u0131l \u00c7al\u0131\u015ft\u0131r\u0131r\u0131z?<\/h5>\n<p>S\u0131ra as\u0131l cevaplanmas\u0131 gereken soruya gelmi\u015ftir.<\/p>\n<p>E\u011fer ki ChildActionOnly attributeu ile i\u015faretlenmi\u015f Action metodunu tetiklemek istiyorsan\u0131z &#8220;Html.RenderAction&#8221; metodunu kullanman\u0131z yeterlidir. Bu metot ile ilgili Action metot \u00e7al\u0131\u015ft\u0131r\u0131l\u0131r, yorumlan\u0131r ve sonu\u00e7 ilgili alana getirtilir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class HomeController : Controller\r\n    {\r\n        public ActionResult Index()\r\n        {\r\n            return View();\r\n        }\r\n\r\n        &#x5B;ChildActionOnly]\r\n        public int Islem()\r\n        {\r\n            return 4 + 2;\r\n        }\r\n    }\r\n<\/pre>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n--- Index.cshtml ---\r\n&lt;div style=&quot;width:250px;&quot;&gt;\r\n    &lt;strong&gt;\u0130\u015flem Sonucu&lt;\/strong&gt; : @{Html.RenderAction(&quot;Islem&quot;, &quot;Home&quot;);}\r\n&lt;\/div&gt;\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funa bakarsan\u0131z e\u011fer Index.cshtml sayfas\u0131 \u00fczerinde Html.RenderAction metodu ile ilgili metodumuz tetiklenmi\u015ftir. Haliyle metot sonucuda tetiklendi\u011fi alana bas\u0131lacakt\u0131r.<\/p>\n<p>Bu attributelar c\u00fczi miktarda da olsa gerekli yerlerde kullan\u0131ld\u0131\u011f\u0131 taktirde bir g\u00fcvenlik mahiyetinde i\u015flev g\u00f6rmektedirler. O y\u00fczden bilmekte her zaman fayda var lakin kullanmak ise her daim tercihinize kalm\u0131\u015f&#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, Biliyorsunuz ki, Asp.NET MVC mimarisinde Controller katman\u0131nda bulunan Action metodlar\u0131 taray\u0131c\u0131m\u0131z\u0131n adres \u00e7ubu\u011fu \u00fczerinden tetikleyebilmekteyiz. Buradan \u00e7\u0131karabilece\u011fimiz sonu\u00e7, server i\u015flemleri dedi\u011fimiz k\u0131sma kullan\u0131c\u0131 isterse adres \u00fczerinden m\u00fcdahale edebilmekte ve bu m\u00fcdahale kritik noktalarda&#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":3014,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[1647,1649,1646,1648],"class_list":["post-5772","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-asp-net-mvc","tag-childactiononly","tag-html-renderaction","tag-nonaction","tag-renderaction"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/5772","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=5772"}],"version-history":[{"count":0,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/5772\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/3014"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=5772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=5772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=5772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}