0

I have been searching a lot for the way that I can handle 404 error for redirect it to a page designed and named 404 error.

Some of the articles say that I should do some changes in Route config. I changed it and now below codes are my route.config but still, it does not work properly

public static void RegisterRoutes(RouteCollection routes)
{
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

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

        //404 ERRORS:
        routes.MapRoute(
            name:"404-PageNotFound",
            url: "{}",
            new { controller = "Home", action = "Pagenotfound" }
        );
}

I mean still, when I run the project and I type the wrong address, it shows default 404 page not the one I designed - Pagenotfound.cshtml.

halfer
  • 19,824
  • 17
  • 99
  • 186
neda Derakhshesh
  • 1,103
  • 2
  • 20
  • 43

1 Answers1

1

Copy and paste the following code between <sytem.web> tags in web.config page. When occuring 404 error, it redirect to Pagenotfound.cshtml page.

<customErrors mode="On">
  <error statusCode="404" redirect="/Error/Pagenotfound"/>
</customErrors>

Also, add [HandleError] attribute on top of Controller pages.

Onur AKKÖSE
  • 84
  • 2
  • 10