khalannz7
Centipat
- Katılım
- 18 Ağustos 2022
- Mesajlar
- 609
- Çözümler
- 8
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: