Search code examples
c#asp.net-mvcredirecttoaction

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.


Solution

  • 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