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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With