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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.