2012-06-23 28 views
7

Tôi nhận được lỗi này "Không thể tìm thấy tuyến đường có tên 'MemberRoute' trong bộ sưu tập tuyến đường. Tên thông số: tên". Dưới đây là Global.asax của tôi,Không thể tìm thấy tên tuyến đường trong bộ sưu tập tuyến đường

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 }); 

Tại sao tên đường không được tìm thấy?

+0

Xem tại đây: http://stackoverflow.com/q/8944355/102937 –

+0

Cũng không nhất thiết là url không được hiển thị chính xác. Tôi đã thử chuyển hướng để điều tuyến và thêm bộ điều khiển/chỉ mục và whatnot nhưng nó làm cho url trông giống như site.com/member?userId=1&pseudoName=jondoe hơn là những gì tôi đang cố gắng để đạt được đó là site.com/member/ 1/jondoe –

Trả lời

7

Hãy đến để tìm hiểu rằng điều này là do MVC 4 và tất cả định tuyến tùy chỉnh đều nằm trong thư mục App_Start trong tệp RouteConfig.cs. Khi tôi mở Global.asax.cs, không có phương thức RegisterRoutes vì ​​vậy tôi đã thêm nó vào bản thân mình và thêm các tuyến tùy chỉnh của tôi nhưng nó không hoạt động. Đã tìm thấy tệp RouteConfig và đã có phương pháp RegisterRoutes với các giá trị mặc định đã được thiết lập. Đã thêm tuyến đường tùy chỉnh của tôi ở đó và nó hoạt động như mong đợi.

Các vấn đề liên quan