Reputation: 1887
I am having Home controller with default action as landing.
But for ErrorController default action should be index
In the RegisterRoutes method in Global.cs, I had written like this : -
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Landing", id = UrlParameter.Optional }
But when I am trying to redirect to error from Application_Error event : -
Exception error = Server.GetLastError();
string redirectUrl = "~/Error/id=" + errorId;
HttpContext.Current.Server.ClearError();
HttpContext.Current.Response.Redirect(redirectUrl);
it is throwing error - action landing not found.
Upvotes: 2
Views: 3499
Reputation: 8347
Put this in your route mapping method before the other one, and it will give the error controller a different default action. Order is important here, routes are matched on a first-come, first-serve basis.
routes.MapRoute(
"Error", // Route name
"Error/{action}/{id}", // URL with parameters
new { controller = "Error", action = "index", id = UrlParameter.Optional }
Upvotes: 0
Reputation: 67336
Just add another route above your current route for the more specific Error case:
routes.MapRoute(
"Error", // Route name
"Error/{action}/{id}", // URL with parameters
new { action = "Index", id = UrlParameter.Optional }
Also, this looks a bit strange:
string redirectUrl = "~/Error/id=" + errorId;
Seems like it would be more this this:
string redirectUrl = "~/Error?id=" + errorId;
Upvotes: 3