﻿
{"id":1128,"date":"2012-12-15T16:46:30","date_gmt":"2012-12-15T16:46:30","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=1128"},"modified":"2012-12-15T16:46:30","modified_gmt":"2012-12-15T16:46:30","slug":"mvc-htmlhelper-metodu-olusturmak-ve-calismak","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/mvc-htmlhelper-metodu-olusturmak-ve-calismak\/","title":{"rendered":"MVC HtmlHelper Metodu Olu\u015fturmak ve \u00c7al\u0131\u015fmak"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Asp.NET MVC, Asp.NET Web Formlar kadar esnek Component deste\u011fi sa\u011flamad\u0131\u011f\u0131ndan dolay\u0131, HtmlHelper s\u0131n\u0131f\u0131n\u0131 kullanarak baz\u0131 kontrolleri kendimiz olu\u015fturmak zorunda kalabiliyoruz.Ancak bu konu \u00e7ok a\u011f\u0131r ve zahmetli bir i\u015fmi\u015f gibi alg\u0131laman\u0131n l\u00fczmu yok.Asl\u0131nda bu konuda olu\u015fturulan metod, hemen hemen her projede yazd\u0131\u011f\u0131m\u0131z s\u0131radan bir metod olacakt\u0131r.Haa tabi, her ne kadar <strong><em>Extension<\/em> <\/strong>metod olsada, en nihayetinde geriye string tipinde html kodlar\u0131 d\u00f6nen metod yazaca\u011f\u0131z.Haliyle, Web Formlar\u0131nda bu tarz ihtiya\u00e7lar\u0131m\u0131z olmad\u0131\u011f\u0131 i\u00e7in biraz bu tip metotlar\u0131 yazmaya uzak kald\u0131\u011f\u0131m\u0131z\u0131 s\u00f6yleyebiliriz.<br \/>\n<!--more--><br \/>\nKonuya giri\u015f yapmadan \u00f6nce, baz\u0131 durumlar\u0131 ele al\u0131p sorgulayaca\u011f\u0131z.<br \/>\n\u00d6ncelikle \u015fu sorulara cevap verelim..<br \/>\n<em><strong>Soru 1 )<\/strong><\/em> Neden html kodlar\u0131n\u0131 manuel olarak bizler yazm\u0131yoruz ve\u00a0for, foreach gibi d\u00f6ng\u00fclerde manuel yazarak, gerekli verileri gerekli alanlar\u0131na atayarak sayd\u0131rm\u0131yoruz? da HtmlHelper metodu yaz\u0131yoruz.<\/p>\n<p>\u00c7\u00fcnk\u00fc, HTML kodunu manuel olarak yazmaktansa, bir programlama \u00f6\u011fesi olan metotlar\u0131 kullanmak daha h\u0131zl\u0131, daha basit ve\u00a0 etkili olacakt\u0131r.Yani, HtmlHelper metodu olu\u015fturmak zorunda de\u011filiz belki ama, yapt\u0131\u011f\u0131m\u0131z i\u015fi, yani programc\u0131l\u0131\u011f\u0131, en iyi ve esnek bir bi\u00e7imde yapmakta bu sanat\u0131n, sanatkarl\u0131\u011f\u0131d\u0131r.<\/p>\n<p><em><strong>Soru 2 )<\/strong><\/em>Neden HtmlHelper s\u0131n\u0131f\u0131nda \u00e7al\u0131\u015ft\u0131r\u0131lacak Extension metod olu\u015fturmak zorunday\u0131m?<\/p>\n<p>Bir kere zorunda oldu\u011fumuz bir durum yok.\u0130stedi\u011finiz isimde bir static s\u0131n\u0131f olu\u015fturup, i\u00e7ine istedi\u011finiz html kodlar\u0131 d\u00f6nen static metodlar yaz\u0131p, o s\u0131n\u0131f ismi \u00fczerinden o metodlar\u0131da \u00e7a\u011f\u0131rabilirsiniz.Bu y\u00f6ntemi kullan\u0131rsak, hi\u00e7bir eksi\u011fimiz veya yanl\u0131\u015f\u0131m\u0131z olmayacakt\u0131r.Ancak, Asp.NET MVC&#8217;de html kontrollerine HtmlHelper s\u0131n\u0131f\u0131ndaki &#8220;Html&#8221; ad\u0131ndaki bir \u00f6zellikten(property) eri\u015febiliriz.Eee haliyle, madem standart olarak MVC&#8217;de var olan html kontrolleri HtmlHelper s\u0131n\u0131f\u0131 i\u00e7erisinde, bende bu d\u00fczeni hi\u00e7 bozmaz, adet olarak devam ettiririm.<br \/>\nHtmlHelper metodu olu\u015fturmam\u0131z bu vicdani se\u00e7im meselesinden ba\u015fka bir durum de\u011fildir.<\/p>\n<p>\u015eimdi basit bir extension metod yazal\u0131m.Bu metodun ald\u0131\u011f\u0131 parametreler sonucu, bir resim nesnesine link vererek sayfam\u0131zda g\u00f6sterelim.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n---ImageLink.cs---\r\n    public static class ImageLink\r\n    {\r\n        public static string Image(this HtmlHelper Html, string ControllerAdi, string ActionAdi, object ParametreDegeri, string ResimYolu)\r\n        {\r\n            return string.Format(&quot;&lt;a href=\\&quot;\/{0}\/{1}\/{2}\\&quot;&gt;&lt;img src=\\&quot;{3}\\&quot;\/&gt;&lt;\/a&gt;&quot;, ControllerAdi, ActionAdi, ParametreDegeri, ResimYolu);\r\n        }\r\n    }\r\n<\/pre>\n<p>Evet g\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi, Extension metodumuzu yazm\u0131\u015f olduk.A\u015fa\u011f\u0131daki gibi kullan\u0131rsak e\u011fer, istedi\u011fimiz sonucu \u00fcretmi\u015f olacakt\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;%= Html.Image(&quot;Home&quot;,&quot;Index&quot;,3,&quot;\/Content\/62305.jpg&quot;)%&gt;\r\n<\/pre>\n<p>E\u011fer projemizi \u00e7al\u0131\u015ft\u0131r\u0131rsak, Extension metodumuzun istedi\u011fimiz nesneyi \u00fcretti\u011fini ve istedi\u011fimiz gibi ekrana yerle\u015ftirdi\u011fini g\u00f6rece\u011fiz.Bu yazd\u0131\u011f\u0131m extension metodu sizler geli\u015ftirebilirsiniz.\u00d6rne\u011fin, width heigth gibi de\u011ferleri parametrik olarak ayarlanabilir yapabilirsiniz.<\/p>\n<p>Bu sefer ki yaz\u0131m\u0131nda sonuna gelmi\u015f bulunmaktay\u0131z.<br \/>\nFaydalanman\u0131z dile\u011fiyle..<br \/>\nBir sonraki yaz\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere..<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar..<\/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 MVC, Asp.NET Web Formlar kadar esnek Component deste\u011fi sa\u011flamad\u0131\u011f\u0131ndan dolay\u0131, HtmlHelper s\u0131n\u0131f\u0131n\u0131 kullanarak baz\u0131 kontrolleri kendimiz olu\u015fturmak zorunda kalabiliyoruz.Ancak bu konu \u00e7ok a\u011f\u0131r ve zahmetli bir i\u015fmi\u015f gibi alg\u0131laman\u0131n l\u00fczmu yok.Asl\u0131nda bu konuda&#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":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[746,289,310,283],"class_list":["post-1128","post","type-post","status-publish","format-standard","hentry","category-asp-net-mvc","tag-asp-net-mvc","tag-html","tag-htmlhelper","tag-mvc"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/1128","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=1128"}],"version-history":[{"count":0,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/1128\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=1128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=1128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=1128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}