2

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.

2 Answers 2

3

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;
Sign up to request clarification or add additional context in comments.

6 Comments

how about this - string redirectUrl = "~/Error/?id=" + errorId;
after adding additional routemap above the default route it throws error - "Value cannot be null or empty.\r\nParameter name: controllerName"
Are you adding the route above the default route? And changing the Route name?
yeah the same way you suggested
try to take out the {action} part of the uri template. "Error/{{id}", // URL with parameters
|
0

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 }

1 Comment

after adding additional routemap above the default route it throws error - "Value cannot be null or empty.\r\nParameter name: controllerName"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.