﻿
{"id":6478,"date":"2017-05-29T03:05:37","date_gmt":"2017-05-29T03:05:37","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=6478"},"modified":"2017-05-29T03:05:37","modified_gmt":"2017-05-29T03:05:37","slug":"asp-net-mvc-ile-custom-error-pageozel-hata-sayfasina-yonlendirme","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/asp-net-mvc-ile-custom-error-pageozel-hata-sayfasina-yonlendirme\/","title":{"rendered":"Asp.NET MVC \u0130le Custom Error Page(\u00d6zel Hata Sayfas\u0131na Y\u00f6nlendirme)"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Asp.NET MVC mimarisiyle in\u015fa edilen projelerde beklenmedik olas\u0131 hatalar al\u0131nd\u0131\u011f\u0131nda yahut yap\u0131land\u0131r\u0131lmam\u0131\u015f, bozuk adreslere kullan\u0131c\u0131lar taraf\u0131ndan talepte bulunuldu\u011funda \u00f6ncelikle yapmam\u0131z gereken sistem taraf\u0131ndan f\u0131rlat\u0131lan hatay\u0131 kullan\u0131c\u0131ya g\u00f6stermeksizin kullan\u0131c\u0131y\u0131 \u00f6zel bir sayfaya y\u00f6nlendirmektir. \u0130\u015fte bu i\u00e7eri\u011fimizde bu i\u015flemin nas\u0131l yap\u0131laca\u011f\u0131n\u0131 ele alacak ve dikkat edilmesi gereken noktalar\u0131 vurgulayaca\u011f\u0131z.<\/p>\n<p>\u015eimdi d\u00fc\u015f\u00fcn\u00fcn; \u00fczerinde \u00e7al\u0131\u015ft\u0131\u011f\u0131m\u0131z projede kullan\u0131c\u0131 taraf\u0131ndan ge\u00e7ersiz bir adrese talepte bulunuldu\u011fu vakit nas\u0131l bir sonu\u00e7la kar\u015f\u0131la\u015f\u0131lmaktad\u0131r?<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/05\/Asp.NET-MVC-\u0130le-Custom-Error-Page\u00d6zel-Hata-Sayfas\u0131na-Y\u00f6nlendirme.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6482\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/05\/Asp.NET-MVC-\u0130le-Custom-Error-Page\u00d6zel-Hata-Sayfas\u0131na-Y\u00f6nlendirme.png\" alt=\"Asp.NET MVC \u0130le Custom Error Page(\u00d6zel Hata Sayfas\u0131na Y\u00f6nlendirme)\" width=\"1077\" height=\"214\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/05\/Asp.NET-MVC-\u0130le-Custom-Error-Page\u00d6zel-Hata-Sayfas\u0131na-Y\u00f6nlendirme.png 1077w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/05\/Asp.NET-MVC-\u0130le-Custom-Error-Page\u00d6zel-Hata-Sayfas\u0131na-Y\u00f6nlendirme-300x60.png 300w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/05\/Asp.NET-MVC-\u0130le-Custom-Error-Page\u00d6zel-Hata-Sayfas\u0131na-Y\u00f6nlendirme-768x153.png 768w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/05\/Asp.NET-MVC-\u0130le-Custom-Error-Page\u00d6zel-Hata-Sayfas\u0131na-Y\u00f6nlendirme-1024x203.png 1024w\" sizes=\"auto, (max-width: 1077px) 100vw, 1077px\" \/><\/a><br \/>\nG\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere &#8220;404 hatas\u0131&#8221; dedi\u011fimiz hatayla kar\u015f\u0131la\u015f\u0131lmaktad\u0131r.<\/p>\n<p>Bu ve buna benzer bir\u00e7ok olas\u0131 hata do\u011furabilecek durumlar i\u00e7in a\u015fa\u011f\u0131daki talimatlar\u0131 izleyerek \u00f6zel hata sayfan\u0131za y\u00f6nlendirmede bulunabilirsiniz.<\/p>\n<p>\u00d6ncelikle yapman\u0131z gereken, hatay\u0131 kar\u015f\u0131layacak olan &#8220;Error(Controller).cs&#8221; isminde bir controller olu\u015fturman\u0131zd\u0131r.(<em>Controller&#8217;\u0131n ad\u0131n\u0131 konuyla alakadar olmak \u015fart\u0131yla istedi\u011finiz \u015fekilde belirtebilirsiniz.<\/em>)<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class ErrorController : Controller\r\n    {\r\n        public ActionResult PageError()\r\n        {\r\n            Response.TrySkipIisCustomErrors = true;\r\n            return View();\r\n        }\r\n        public ActionResult Page404()\r\n        {\r\n            Response.StatusCode = 404;\r\n            Response.TrySkipIisCustomErrors = true;\r\n            return View(&quot;PageError&quot;);\r\n        }\r\n        public ActionResult Page403()\r\n        {\r\n            Response.StatusCode = 403;\r\n            Response.TrySkipIisCustomErrors = true;\r\n            return View(&quot;PageError&quot;);\r\n        }\r\n        public ActionResult Page500()\r\n        {\r\n            Response.StatusCode = 500;\r\n            Response.TrySkipIisCustomErrors = true;\r\n            return View(&quot;PageError&quot;);\r\n        }\r\n    }\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funda g\u00f6r\u00fcld\u00fc\u011f\u00fc gibi &#8220;Error(Controller).cs&#8221; i\u00e7erisine akl\u0131ma gelen t\u00fcm hatalar\u0131 kar\u015f\u0131lamay\u0131 sa\u011flayacak action metotlar\u0131 olu\u015fturdum.(<em>Tabi ki de siz daha fazlas\u0131n\u0131 olu\u015fturabilirsiniz.<\/em>)<\/p>\n<p>T\u00fcm actionlar &#8220;PageError&#8221; sayfas\u0131na d\u00f6n\u00fc\u015f yapaca\u011f\u0131 i\u00e7in &#8220;PageError.cshtml&#8221; sayfas\u0131n\u0131da a\u015fa\u011f\u0131daki gibi olu\u015fturuyorum.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;h2&gt;Sayfa bulunamad\u0131...&lt;\/h2&gt;\r\n&lt;div&gt;\r\n    Hata Kodu : @Response.StatusCode\r\n&lt;\/div&gt;\r\n&lt;div&gt;\r\n    Hata Sebebi : @ViewBag.Kaynak\r\n&lt;\/div&gt;\r\n<\/pre>\n<p>Bu i\u015flemden sonra ana dizindeki Web.config dosyas\u0131n\u0131 a\u00e7\u0131n\u0131z ve &lt;system.web&gt;&#8230;&lt;\/system.web&gt; nodelar\u0131 i\u00e7erisine a\u015fa\u011f\u0131daki kal\u0131b\u0131 yerle\u015ftiriniz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\n.\r\n  &lt;system.web&gt;\r\n    &lt;customErrors mode=&quot;On&quot;&gt;\r\n      &lt;error statusCode=&quot;404&quot; redirect=&quot;\/Error\/Page404&quot;\/&gt;\r\n    &lt;\/customErrors&gt;\r\n    &lt;compilation debug=&quot;true&quot; targetFramework=&quot;4.6.1&quot;\/&gt;\r\n    &lt;httpRuntime targetFramework=&quot;4.6.1&quot;\/&gt;\r\n  &lt;\/system.web&gt;\r\n.\r\n.\r\n.\r\n<\/pre>\n<p>Dikkat ederseniz burada &#8220;customErrors&#8221; nodeu ile \u00f6zel hata kontrol\u00fcn\u00fc devreye sokmu\u015f bulunuyoruz. &#8220;error&#8221; nodu ile de &#8220;statusCode&#8221; u 404 olan hata i\u00e7in &#8220;\/Error\/Page404&#8221; sayfas\u0131na y\u00f6nlendirmede bulunuyoruz.<\/p>\n<p>Bu i\u015flemden sonra projemizi derleyip \u00e7al\u0131\u015ft\u0131ral\u0131m ve bilin\u00e7li olarak 404 hatas\u0131n\u0131 alarak s\u00fcreci yeniden g\u00f6zlemleyelim.<\/p>\n<p><iframe loading=\"lazy\" width=\"260\" height=\"146\" src=\"https:\/\/www.youtube.com\/embed\/iLV6TzluwAM\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p>Videodan da g\u00f6rd\u00fc\u011f\u00fcn\u00fcz \u00fczere olas\u0131 bir hata durumunda Web.config&#8217;te yapt\u0131\u011f\u0131m\u0131z d\u00fczenleme devreye girmekte ve verilen adrese ilgili kullan\u0131c\u0131 y\u00f6nlendirilmektedir.<\/p>\n<p>Burada dikkatinizi bir hususa \u00e7ekmek istiyorum. Videoda da g\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere y\u00f6nlendirme yap\u0131l\u0131rken &#8220;<em>http:\/\/localhost:****\/Error\/Page404?aspxerrorpath=\/gecersizadres<\/em>&#8221; \u015feklinde kar\u0131\u015f\u0131k ve MVC&#8217;nin yap\u0131s\u0131na uymayan bir tarzda adres olu\u015fturulmu\u015ftur. Adresteki &#8220;aspxerrorpath&#8221; parametresi hangi de\u011ferin 404 hatas\u0131na sebep oldu\u011funu g\u00f6stermektedir. E\u011fer ki bu adresi ve gelen &#8220;aspxerrorpath&#8221; parametresini y\u00f6netmek istiyorsan\u0131z a\u015fa\u011f\u0131daki gibi bir \u00e7al\u0131\u015fma yapman\u0131z yeterli olacakt\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class ErrorController : Controller\r\n    {\r\n.\r\n.\r\n.\r\n        public ActionResult Page404(string aspxerrorpath)\r\n        {\r\n            if (!string.IsNullOrEmpty(aspxerrorpath))\r\n                ViewBag.Kaynak = aspxerrorpath;\r\n            Response.StatusCode = 404;\r\n            Response.TrySkipIisCustomErrors = true;\r\n            return View(&quot;PageError&quot;);\r\n        }\r\n.\r\n.\r\n.\r\n    }\r\n<\/pre>\n<p><a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/05\/Asp.NET-MVC-\u0130le-Custom-Error-Page\u00d6zel-Hata-Sayfas\u0131na-Y\u00f6nlendirme-1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/05\/Asp.NET-MVC-\u0130le-Custom-Error-Page\u00d6zel-Hata-Sayfas\u0131na-Y\u00f6nlendirme-1.png\" alt=\"Asp.NET MVC \u0130le Custom Error Page(\u00d6zel Hata Sayfas\u0131na Y\u00f6nlendirme)\" width=\"385\" height=\"203\" class=\"aligncenter size-full wp-image-6496\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/05\/Asp.NET-MVC-\u0130le-Custom-Error-Page\u00d6zel-Hata-Sayfas\u0131na-Y\u00f6nlendirme-1.png 385w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/05\/Asp.NET-MVC-\u0130le-Custom-Error-Page\u00d6zel-Hata-Sayfas\u0131na-Y\u00f6nlendirme-1-300x158.png 300w\" sizes=\"auto, (max-width: 385px) 100vw, 385px\" \/><\/a><\/p>\n<p>Velhas\u0131l, olu\u015fan olas\u0131 hatalar\u0131 bu \u015fekilde Web.config ile y\u00f6nlendirebilmekteyiz. Ayriyetten olas\u0131 hatalar\u0131,  &#8220;Global.asax&#8221; dosyas\u0131ylada y\u00f6nlendirebiliriz. &#8220;Global.asax&#8221; dosyas\u0131, Asp.NET mimarisinde etkili bir hata y\u00f6netimi(Error Handling) sunmaktad\u0131r. Uygulamadaki t\u00fcm request(talep) ve response(cevap)lar\u0131n y\u00f6netildi\u011fi ve bunlara ait eventlar\u0131n yakaland\u0131\u011f\u0131 yer &#8220;Global.asax&#8221; dosyas\u0131d\u0131r. \u0130lgili dosya i\u00e7erisinde &#8220;Application_Error()&#8221; metodu, uygulaman\u0131n herhangi bir yerinde herhangi bir kaynaktan dolay\u0131 meydana gelen hatalar\u0131 yakalamakla m\u00fckelleftir. \u015eimdi gelin birde olay\u0131 bu yap\u0131yla ele alal\u0131m.<\/p>\n<p>\u00d6ncelikle RouteConfig dosyas\u0131na a\u015fa\u011f\u0131daki gibi bir route \u015femas\u0131 ekleyelim.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class RouteConfig\r\n    {\r\n        public static void RegisterRoutes(RouteCollection routes)\r\n        {\r\n.\r\n.\r\n.\r\n            routes.MapRoute(\r\n              name: &quot;Hata&quot;,\r\n              url: &quot;hata\/{kod}&quot;,\r\n              defaults: new { controller = &quot;Error&quot;, action = &quot;Page404&quot;, kod = UrlParameter.Optional }\r\n          );\r\n.\r\n.\r\n.\r\n        }\r\n    }\r\n<\/pre>\n<p>Bu route \u015femas\u0131 birazdan &#8220;Global.asax&#8221; dosyas\u0131ndan yapaca\u011f\u0131m\u0131z y\u00f6nlendirmenin adres \u015femas\u0131 olacakt\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class MvcApplication : System.Web.HttpApplication\r\n    {\r\n        protected void Application_Start()\r\n        {\r\n            AreaRegistration.RegisterAllAreas();\r\n            RouteConfig.RegisterRoutes(RouteTable.Routes);\r\n        }\r\n\r\n        protected void Application_Error(object sender, EventArgs e)\r\n        {\r\n            Exception ex = Server.GetLastError();\r\n            if (ex is HttpException &amp;&amp; ((HttpException)ex).GetHttpCode() == 404)\r\n            {\r\n                Response.Redirect(&quot;hata\/404&quot;);\r\n            }\r\n        }\r\n    }\r\n<\/pre>\n<p>Evet&#8230; G\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere olas\u0131 hata al\u0131nd\u0131\u011f\u0131 an Application_Error() metodu devreye girecek ve ilgili hatan\u0131n kodu denetlenip kullan\u0131c\u0131 yukar\u0131da olu\u015fturdu\u011fumuz routea uygun adrese y\u00f6nlendirilecektir.<\/p>\n<p><iframe loading=\"lazy\" width=\"260\" height=\"146\" src=\"https:\/\/www.youtube.com\/embed\/tlfENlEYzcg\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p>Sonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar dilerim&#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, Asp.NET MVC mimarisiyle in\u015fa edilen projelerde beklenmedik olas\u0131 hatalar al\u0131nd\u0131\u011f\u0131nda yahut yap\u0131land\u0131r\u0131lmam\u0131\u015f, bozuk adreslere kullan\u0131c\u0131lar taraf\u0131ndan talepte bulunuldu\u011funda \u00f6ncelikle yapmam\u0131z gereken sistem taraf\u0131ndan f\u0131rlat\u0131lan hatay\u0131 kullan\u0131c\u0131ya g\u00f6stermeksizin kullan\u0131c\u0131y\u0131 \u00f6zel bir sayfaya y\u00f6nlendirmektir. \u0130\u015fte&#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":3453,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[1829,1834,746,1831,1828,1826,1830,1832,286,1833,1827],"class_list":["post-6478","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-asp-net-mvc","tag-404-hatasi","tag-application_error","tag-asp-net-mvc","tag-asp-net-mvc-global-asax-ile-hata-yonlendirme","tag-asp-net-mvc-hata-yonlendirme","tag-custom-error-page","tag-customerrors","tag-error-handling","tag-global-asax","tag-hata-yonetimi","tag-ozel-hata-sayfasina-yonlendirme"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/6478","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=6478"}],"version-history":[{"count":0,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/6478\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/3453"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=6478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=6478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=6478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}