Cannot find route name in route collection

I get this error "A route with the name" MemberRoute "was not 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 found?

+7
source share
1 answer

Come to find out that this is related to MVC 4 and that all user routing is located in the App_Start folder in the RouteConfig.cs file. When I opened Global.asax.cs, there was no RegisterRoutes method, so I added it myself and added my custom routes, but it did not work. The RouteConfig file was found, and it already was, the RegisterRoutes method with the default settings already configured. Added my own route there, and it works as expected.

+7
source

All Articles