Chen Chen
Chen Chen

Reputation: 182

ASP.NET MVC RedirectToAction not redirecting to Index of another controller

In my Account/Login controller method, I have something like:

var classA = GetObject(); //actual code omitted
switch(classA.PropA)
{
    case 1:
        return RedirectToAction("Action2", "Registration");
    //more case code omitted
    default:
        return RedirectToAction("Index", "Registration");
}

All the cases work fine in the switch block except the default where it's suppose to go to Index in RegistrationController. Instead of that, it takes me to localhost:port/Registration, where the action Index is omitted.

It works fine if the ActionName is changed to something else - Index2 for example. Also works fine if the controller name is changed to something else.

RouteConfig is just the auto-generated code from creating the project, which is as follows:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
     }

Thanks in advance.

Upvotes: 2

Views: 3312

Answers (3)

Usman
Usman

Reputation: 4693

There is nothing wrong with the route setting the reason it does not include Index in the URL because according to default route

url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

when you enter /Registration the route already knows the default action which is index so it does not add /index in URL and about

403.14 forbidden

if you look at this post HTTP Error 403.14 - Forbidden - MVC 4 with IIS Express it could be because you might have a file or folder named Registration in the project directory

Upvotes: 4

Hicham Bouchikhi
Hicham Bouchikhi

Reputation: 726

If you use the RedirectionToAction("Index","ControllerName"); it will redirect you with the default mapping config to localhost:port/ControllerName and in your case if you execute the RedirectionToAction("Index","ControllerName"); it will redirect you to localhost:port/Registration

Upvotes: 1

Azhar
Azhar

Reputation: 117

Try to use on

return RedirectToAction("Index");

and in RouteConfig you can route Action "Index" to Controller "Registration"

like

routes.MapRoute("Index", "Index", new { controller = "Registration", action = "Index" });

Upvotes: 0

Related Questions