Ravi Ram
Ravi Ram

Reputation: 24488

ASP.NET MVC RedirectToAction Default Action

How do I redirect to a Controller without defining the Action? (it will default to the default action as defined in the routes config)

Example return RedirectToAction("Index", "Error" new { messageId = errorMessageId} );

Url displayed to user is: http:mydomain/Error/Index/1001

Preferred return RedirectToAction(null, "Error" new { messageId = errorMessageId} );

URL I want to displayed to user is: http:mydomain/Error/1001

When I type the URL manually, it works fine, so I know my routes are working.

I just cant figure out how to do it using Redirectxxxx in MVC.

Upvotes: 0

Views: 1038

Answers (2)

NightOwl888
NightOwl888

Reputation: 56849

You can use RedirectToRoute.

return RedirectToRoute(new { controller = "Error", messageId = errorMessageId });

Alternatively, there is Redirect coupled with Url.RouteUrl.

return Redirect(Url.RouteUrl(new { controller = "Error", messageId = errorMessageId }));

Upvotes: 1

hdrdiab
hdrdiab

Reputation: 343

  protected internal RedirectToAction(
    string actionName,
    string controllerName,
    object routeValues
)

Here is the RedirectToAction method you can define your own route here without using the action name.

For more info about routing check this: Routing

Upvotes: 0

Related Questions