7

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?

Shane LeBlanc
  • 2,633
  • 13
  • 42
  • 74
  • See here: http://stackoverflow.com/q/8944355/102937 – Robert Harvey Jun 24 '12 at 00:04
  • Well it's not necessarily the url not being displayed properly. I tried the redirect to route thing and adding the controller/index and whatnot but it makes the url look like site.com/member?userId=1&pseudoName=jondoe rather than what I'm trying to achieve which is site.com/member/1/jondoe – Shane LeBlanc Jun 24 '12 at 00:10

2 Answers2

8

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.

Shane LeBlanc
  • 2,633
  • 13
  • 42
  • 74
0

When I was first learning MVC, I would try to test my [HttpGet] action and sometimes got this. When I googled the error message, I got nothing helpful.

Only to then realise it was the POST route that was missing, in the error message, and I hadn't created the [HttpPost] action in my controller yet, only the [HttpGet] one.

I hope this helps someone.

MGOwen
  • 6,562
  • 13
  • 58
  • 67