Yargicx
Yargicx

Reputation: 1734

ASP.NET MVC 5 Custom Error Pages Doesn't Work

I've set up my webconfig to custom error pages. But it doesn't work. I create a controller and action with name "hata". I can see this page "http://localhost/hata/bulunamadi" but when i try to open not exists page so my custom error pages doesn't show. (i see iss default 404 page)

<system.web>    
<customErrors defaultRedirect="~/hata/bulunamadi" redirectMode="ResponseRewrite" mode="On">
  <error statusCode="404" redirect="~/hata/bulunamadi"/>
</customErrors>
</system.web>

Upvotes: 0

Views: 1502

Answers (3)

Mohammad Negah
Mohammad Negah

Reputation: 119

Please follow step by step this description : First, you have to settings up your web config for custom page error. Just like this

<system.web>
   <customErrors mode="On" defaultRedirect="~/Error/" redirectMode="ResponseRedirect">
      <error statusCode="404" redirect="~/Error/Error404/" />
      <error statusCode="500" redirect="~/Error/Error500/" />
      <error statusCode="400" redirect="~/Error/Error400/" />
   </customErrors>
</system.web>

Then you have to configure your RouteConfig.cs :

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute(
    "404-PageNotFound",
    "{*url}",
    new { controller = "Error", action = "Error404" }
);

And finally, you have to create your custom error View and Action :

public class ErrorController : Controller
    {
        // GET: Error
        public ActionResult Index()
        {
            return View();
        }

        public ViewResult Error404()
        {
            return View();
        }

        public ViewResult Error500()
        {
            return View();
        }

        public ViewResult Error400()
        {
            return View();
        }

        public ActionResult General()
        {
            return View();
        }
    }

Upvotes: 0

Vinit Patel
Vinit Patel

Reputation: 2474

<customErrors mode="On" defaultRedirect="~/Error">
  <error redirect="~/Error/NotFound" statusCode="404" />
</customErrors>

And the controller contains the following:

public class ErrorController : Controller
{
    public ViewResult Index()
    {
        return View("Error");
    }
    public ViewResult NotFound()
    {
        Response.StatusCode = 404;  //you may want to set this to 200
        return View("NotFound");
    }
}

Upvotes: 2

GANI
GANI

Reputation: 2059

try this

Web.Config

 <system.web>
 <customErrors mode="On" defaultRedirect="Error">
  <error statusCode="404" redirect="NotFound" />
 </customErrors>   
</system.web>

create Error.cshtml and NotFound.cshtml in shared folder Create an ErrorController

    public ActionResult Error()
    {
     return View();
    }

Upvotes: 0

Related Questions