ana sayfa > MVC > Asp.NET MVC Hata Sayfasına Yönlendirme

Asp.NET MVC Hata Sayfasına Yönlendirme

ErrorController 

public class ErrorController : Controller
{
        public ActionResult ErrorPage(Exception exception)
        {
            Response.TrySkipIisCustomErrors = true;
            return View();
        }
        public ActionResult ErrorPage404()
        {
            Response.StatusCode = 404;
            Response.TrySkipIisCustomErrors = true;
            return View();
        }
        public ActionResult ErrorPage403()
        {
            Response.StatusCode = 403;
            Response.TrySkipIisCustomErrors = true;
            return View();
        }
}

web.config ayarları

<system.webServer>
    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>

    <httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="404" subStatusCode="-1"/>
      <error statusCode="404" path="Error/ErrorPage404" responseMode="ExecuteURL"/>
      <remove statusCode="403" subStatusCode="-1"/>
      <error statusCode="403" path="Error/ErrorPage403" responseMode="ExecuteURL"/>
    </httpErrors>
  </system.webServer>

<system.web>
    <customErrors mode="RemoteOnly" defaultRedirect="Error">
      <error statusCode="404" redirect="ErrorPage404"/>
      <error statusCode="403" redirect="ErrorPage403"/>
    </customErrors>
</system.web>

Global.asax ayarları

protected void Application_EndRequest()
{
	if (Context.Response.StatusCode == 404)
	{
		Response.Clear();

		var rd = new RouteData();
		rd.Values["controller"] = "Error";
		rd.Values["action"] = "ErrorPage404";

		IController c = new ErrorController();
		c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
	}
	else if (Context.Response.StatusCode == 403)
	{
		Response.Clear();

		var rd = new RouteData();
		rd.Values["controller"] = "Error";
		rd.Values["action"] = "ErrorPage403";

		IController c = new ErrorController();
		c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
	}
}
Kaynal: https://forum.donanimhaber.com/m_125710795/printable.htm
Categories: MVC Tags:
  1. şimdilik yorum yok.
  1. şimdilik geri bağlantı yok
yorum yapabilmek için giriş yapmalısınız