1

i am trying to route error Page not found in my application, if any route is wrong then display Error Route.

my default route below

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

2 Answers 2

1

Add this in web.config File.

 <system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="404" />
  <error statusCode="404" responseMode="ExecuteURL" path="/Error" />
  <remove statusCode="500" />
  <error statusCode="500" responseMode="ExecuteURL" path="/Error" />
</httpErrors>
<modules>
  <remove name="FormsAuthentication" />
</modules>
Sign up to request clarification or add additional context in comments.

Comments

1

The dot (.) will cause a conflict. use alpha characters for the parameter name,

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

//Catch-All InValid (NotFound) Routes
routes.MapRoute(
    name: "NotFound",
    url: "{*url}",
    defaults: new { controller = "Error", action = "PageNotFound" }
);      

It would also allow you to get the parameter in the action as well

public class ErrorController : Controller {
    public ActionResult PageNotFound(string url) { ... }
}

in case you wanted to do anything else with the value. ie logging, audit...etc

1 Comment

thanks for replay but it's not working,when i run application then i want every time open my default path Home Page, if i open other path like userlist or edit page then it's go to Page404 view

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.