Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't Find Route Name In Route Collection

I am receiving this error "A route named 'MemberRoute' could not be found in the route collection. Parameter name: name". Here is my Global.asax,

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapRoute(
            "MemberRoute",                       // routeName
            "member/{userId}/{pseudoName}", // url
            new
            {                           // url defaults
                controller = "Member",
                action = "Index",
                userId = 0,
                pseudoName = UrlParameter.Optional
            },
            new
            {                          // url constraints
                userId = @"\d+" // must match url {userId}
            }
        );
    }

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

MemberController,

public ActionResult Index(int userId, string pseudoName)
    {
        User user;
        var unitOfWork = new UnitOfWork();
        user = unitOfWork.UserRepository.GetById(userId);

        var expectedName = user.PseudoName.ToSeoUrl();
        var actualName = (pseudoName ?? "").ToLower();

        // permanently redirect to the correct URL
        if (expectedName != actualName)
            return RedirectToActionPermanent("Index", "Member", new { id = user.UserId, pseudoName = expectedName });
        return View(user);
    }

Caller,

return RedirectToRoute("MemberRoute", new { userId = user.UserId, pseudoName = user.PseudoName });

Why is the route name not being found?

like image 586
Shane LeBlanc Avatar asked Jun 23 '12 23:06

Shane LeBlanc


1 Answers

Come to find out that this is due to MVC 4 and that all custom routing is located in the App_Start folder within the RouteConfig.cs file. When I opened up Global.asax.cs there was no RegisterRoutes method so I added it myself and added my custom routes but it did not work. Found the RouteConfig file and there it was already, the RegisterRoutes method with the defaults already setup. Added my custom route there and it works as expected.

like image 99
Shane LeBlanc Avatar answered Nov 15 '22 08:11

Shane LeBlanc