Çözüldü MVC hata yönetimi

Bu konu çözüldü olarak işaretlenmiştir. Çözülmediğini düşünüyorsanız konuyu rapor edebilirsiniz.

khalannz7

Centipat
Katılım
18 Ağustos 2022
Mesajlar
522
Çözümler
7
Yer
İstanbul
Daha fazla  
Cinsiyet
Erkek
Merhaba, hataları düzgün şekilde yönetmek istiyorum kullanıcının karşısına kendi oluşturduğum sayfaları çıkartmak istiyorum. Ama sorun şu ki sitenin içinde Ajax ile yaptığım işlemlerden hata alırsam, bu sayfalara yönlendirmek istemiyorum. Çünkü Catch'den JSON mesajı döndürüyorum ve bir JavaScript kütüphanesi ile bu mesajı gösteriyorum. Ama sorun şu ki customerros'a yazdığım sayfa her şekilde çıkıyor ve JavaScript ile gösterdiğim mesajı eziyor. Aşağıdaki gibi karmaşık şekilde yaptım bilgisi olan aydınlatabilir mi?
Kod:
 protected void Application_Error(object sender, EventArgs e)
        {
            Exception exception = Server.GetLastError();
            bool isAjaxRequest = Context.Request.Headers["X-Requested-With"] == "XMLHttpRequest";

            if (!isAjaxRequest)
            {
                if (exception is HttpException httpException)
                {
                    int httpCode = httpException.GetHttpCode();
                    if (httpCode == 404)
                    {
                        Response.Redirect("~/Error/NotFound");
                    }
                    else if (httpCode == 500)
                    {
                        Response.Redirect("~/Error/ServerError");
                    }
                    else
                    {
                        Response.Redirect("~/Error/Error");
                    }
                }
                else
                {
                    Response.Redirect("~/Error/Error");
                }
            }
        }
Kod:
<customErrors mode="On" defaultRedirect="~/Error/Error">
            <error statusCode="404" redirect="~/Error/NotFound"/>
            <error statusCode="500" redirect="~/Error/ServerError"/>
        </customErrors>
Kod:
public class ErrorController : Controller
    {
        [HttpGet]
        public ActionResult Error()
        {
            return View();
        }

        [HttpGet]
        public ActionResult NotFound()
        {
            Response.StatusCode = 404;
            Response.TrySkipIisCustomErrors = true;
            return View();
        }

        [HttpGet]
        public ActionResult ServerError()
        {
            Response.StatusCode = 500;
            Response.TrySkipIisCustomErrors = true;
            return View();
        }
    }
 
Son düzenleyen: Moderatör:
Çözüm
Global.asax.cs’te Route kayıta şunu ekleyip dener misiniz?
C#:
routes.MapRoute("Error", "{*url}",
    new { controller = "Error", action = "Error" }
);

Framework MVC ile çalışmadım ama Application_Error tetiklenmiyorsa uygulama o isteği URL’den sebep işlemiyor olabilir.

Yeni konular

Geri
Yukarı