Naimish Mungara
Naimish Mungara

Reputation: 237

Asp.Net Mvc Route - Error Route

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

Answers (2)

Nkosi
Nkosi

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

Supraj V
Supraj V

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

Related Questions