chuckd
chuckd

Reputation: 14520

Redirect To Action method not mapping correctly

I'm calling RedirectToAction but it isn't working properly.

I want the resulting URL to look like this:

https://localhost:44301/ManageSpaces/123/overview

but it looks like this and is missing the action portion of the URL:

https://localhost:44301/ManageSpaces/123

Here is my RedirectToAction call.

return RedirectToAction("overview", new RouteValueDictionary(
                new {controller = "ManageSpaces", action = "overview", id = 123}));

Here is what my route looks like in RouteConfig:

 routes.MapRoute("ManageSpaces",
            "ManageSpaces/{id}/{action}",
            new { controller = "ManageSpaces", action = "overview"},
            new { id = @"\d+" } //The regular expression \d+ matches one or more integers
            );

Upvotes: 0

Views: 191

Answers (2)

NightOwl888
NightOwl888

Reputation: 56849

You have made your action route value optional by providing a default value. Optional values are ignored when resolving the URL.

 routes.MapRoute("ManageSpaces",
        "ManageSpaces/{id}/{action}",
        new { controller = "ManageSpaces", action = "overview"},
        new { id = @"\d+" } //The regular expression \d+ matches one or more integers
        );

If you want to include the action in the URL, you have to make it a required argument.

 routes.MapRoute("ManageSpaces",
        "ManageSpaces/{id}/{action}",
        new { controller = "ManageSpaces"},
        new { id = @"\d+" } //The regular expression \d+ matches one or more integers
        );

Upvotes: 0

Maybe it is taking the default route. Rename, remove, or comment out the default route to see if that has any effect.

Upvotes: 1

Related Questions