﻿
{"id":10222,"date":"2019-01-27T22:40:56","date_gmt":"2019-01-27T22:40:56","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=10222"},"modified":"2019-01-27T22:40:56","modified_gmt":"2019-01-27T22:40:56","slug":"asp-net-coreda-ihostingenvironment-ile-server-mappathe-alternatif-yol","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/asp-net-coreda-ihostingenvironment-ile-server-mappathe-alternatif-yol\/","title":{"rendered":"Asp.NET Core&#8217;da IHostingEnvironment \u0130le Server.MapPath&#8217;e Alternatif Yol"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Hat\u0131rlar m\u0131s\u0131n\u0131z? Klasik Asp.NET(Web Forms ya da MVC)&#8217;de server&#8217;a ait i\u015flemlerimizi genellikle &#8220;HttpContext&#8221; s\u0131n\u0131f\u0131 \u00fcyelerinden olan &#8220;Server&#8221; property&#8217;si arac\u0131l\u0131\u011f\u0131yla ger\u00e7ekle\u015ftiriyorduk. Bu i\u015flemler aras\u0131nda en \u00e7ok kulland\u0131\u011f\u0131m\u0131z i\u015flevsellik ise bizlere server&#8217;\u0131n k\u00f6k dizinini veren &#8220;MapPath&#8221; metodu oluyordu. G\u00fcnler ge\u00e7ti, aylar ge\u00e7ti, zaman su gibi ak\u0131p gitti&#8230; Asp.NET Core mimarisi geldi. Lakin i\u00e7erisinden &#8220;Server.MapPath&#8221; kald\u0131r\u0131ld\u0131, gitti. \u0130\u015fte bizler bu i\u00e7eri\u011fimizde Asp.NET Core projelerinde Server.MapPath&#8217;e ihtiyac\u0131m\u0131z oldu\u011funda alternatif olarak bizlere ayn\u0131 hizmeti sa\u011flayacak olan IHostingEnvironment interface&#8217;ini inceliyor olaca\u011f\u0131z.<\/p>\n<p>IHostingEnvironment interface&#8217;i, Asp.NET Core mimarisi dahilinde gelen ve projenin bulundu\u011fu sunucu hakk\u0131nda bizlere gerekli ortamsal bilgileri getirecek alanlar\u0131 bar\u0131nd\u0131ran bir yap\u0131ya sahiptir. Yap\u0131sal olarak .NET Core \u00e7ekirde\u011finin f\u0131trat\u0131nda olan Dependency Injection \u00f6zelli\u011fi sayesinde .NET Core&#8217;a dahil oldu\u011fu i\u00e7in hi\u00e7bir tan\u0131mlamada bulunmaks\u0131z\u0131n direkt olarak constructer&#8217;dan talep edilebilen IHostingEnvironment interface&#8217;inin projelerde a\u015fa\u011f\u0131daki gibi kullan\u0131lmas\u0131 yeterli olacakt\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class HomeController : Controller\r\n    {\r\n        IHostingEnvironment _hostingEnvironment;\r\n        public HomeController(IHostingEnvironment hostingEnvironment)\r\n        {\r\n            _hostingEnvironment = hostingEnvironment;\r\n        }\r\n        public IActionResult Index()\r\n        {\r\n            var hostingInformation = _hostingEnvironment;\r\n            return View();\r\n        }\r\n    }\r\n<\/pre>\n<p>Interface&#8217;e gelen nesnenin bizlere getirdi\u011fi ortamsal bilgilere g\u00f6z atarsak e\u011fer;<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/01\/Asp.NET-Core\u2019da-IHostingEnvironment-\u0130le-Server.MapPath\u2019e-Alternatif-Yol.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/01\/Asp.NET-Core\u2019da-IHostingEnvironment-\u0130le-Server.MapPath\u2019e-Alternatif-Yol.png\" alt=\"Asp.NET Core\u2019da IHostingEnvironment \u0130le Server.MapPath\u2019e Alternatif Yol\" width=\"1256\" height=\"372\" class=\"aligncenter size-full wp-image-10227\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/01\/Asp.NET-Core\u2019da-IHostingEnvironment-\u0130le-Server.MapPath\u2019e-Alternatif-Yol.png 1256w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/01\/Asp.NET-Core\u2019da-IHostingEnvironment-\u0130le-Server.MapPath\u2019e-Alternatif-Yol-300x89.png 300w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/01\/Asp.NET-Core\u2019da-IHostingEnvironment-\u0130le-Server.MapPath\u2019e-Alternatif-Yol-768x227.png 768w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/01\/Asp.NET-Core\u2019da-IHostingEnvironment-\u0130le-Server.MapPath\u2019e-Alternatif-Yol-1024x303.png 1024w\" sizes=\"auto, (max-width: 1256px) 100vw, 1256px\" \/><\/a><br \/>\ng\u00f6r\u00fcld\u00fc\u011f\u00fc gibi uygulaman\u0131n ad\u0131ndan tutun, sunucuda ki root klas\u00f6r\u00fcne kadar t\u00fcm yol verileri bizlere nesnesel olarak d\u00f6nd\u00fcr\u00fclmektedir.<\/p>\n<p>Burada &#8220;ContentRootPath&#8221; \u00f6zelli\u011fine dikkat edelim. G\u00f6r\u00fcld\u00fc\u011f\u00fc gibi ilgili \u00f6zellik uygulaman\u0131n ana dizinini bizlere getirmektedir. Bizler, uygulamam\u0131z\u0131n ana dizinine &#8220;Program.cs&#8221; dosyas\u0131 \u00fczerinden &#8220;UseContentRoot&#8221; metodu ile a\u015fa\u011f\u0131daki gibi m\u00fcdahalede bulunabiliriz;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class Program\r\n    {\r\n        public static void Main(string&#x5B;] args)\r\n        {\r\n            CreateWebHostBuilder(args).Build().Run();\r\n        }\r\n\r\n        public static IWebHostBuilder CreateWebHostBuilder(string&#x5B;] args) =&gt;\r\n            WebHost.CreateDefaultBuilder(args)\r\n                .UseContentRoot(&quot;C:\\\\Users\\\\mebitech\\\\source\\\\repos\\\\IHostingEnvironmentExample\\\\IHostingEnvironmentExample&quot;)\r\n                .UseStartup&lt;Startup&gt;();\r\n    }\r\n<\/pre>\n<p>Dolay\u0131s\u0131yla bu \u015fekilde ilgili yolu kendimize g\u00f6re ki\u015fiselle\u015ftirebiliriz.<\/p>\n<p>\u0130lgilenenlerin faydalanmas\u0131 dile\u011fiyle.<br \/>\nSonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere.<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, Hat\u0131rlar m\u0131s\u0131n\u0131z? Klasik Asp.NET(Web Forms ya da MVC)&#8217;de server&#8217;a ait i\u015flemlerimizi genellikle &#8220;HttpContext&#8221; s\u0131n\u0131f\u0131 \u00fcyelerinden olan &#8220;Server&#8221; property&#8217;si arac\u0131l\u0131\u011f\u0131yla ger\u00e7ekle\u015ftiriyorduk. Bu i\u015flemler aras\u0131nda en \u00e7ok kulland\u0131\u011f\u0131m\u0131z i\u015flevsellik ise bizlere server&#8217;\u0131n k\u00f6k dizinini veren&#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":[2679,2744,2743,2742,666],"class_list":["post-10222","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-server-mappath","tag-asp-net-coreda-ihostingenvironment","tag-ihostingenvironment","tag-server-mappath"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/10222","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=10222"}],"version-history":[{"count":9,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/10222\/revisions"}],"predecessor-version":[{"id":10232,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/10222\/revisions\/10232"}],"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=10222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=10222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=10222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}