Search code examples
asp.net-mvcredirecttoaction

RedirectToAction - but I want it to go to http://example.com/controller/action/id?


I'd like to move an asp.net mvc response to

http://example.com/emails/list/[email protected]

Using RedirectToAction("list", "emails", new { id = "[email protected]"}); takes you to http://example.com/emails/[email protected].

What am I doing wrong?

Thanks,

Rob


Solution

  • Seems like misconfigured routing. Your RegisterRoutes method in Global.asax.cs should look like this:

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Account", action = "Login", id = "" }  // Parameter defaults
            );
    
        }
    

    In line "{controller}/{action}/{id}" presence of {id} means that is going to be substituted by it's value.

    Any other parameter that is not present in routing string would be decoded as ?some_param=value