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
20.2k20 gold badges111 silver badges208 bronze badges
asked Nov 8, 2020 at 15:56

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.

answered Nov 8, 2020 at 16:07
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.