Reputation: 237
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" }
);
Upvotes: 1
Views: 3387
Reputation: 246998
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
Upvotes: 1
Reputation: 977
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>
Upvotes: 1