2010-09-27 154 views
3

我在MembersAreaRegistration文件中的區域,稱爲會員及以下注冊路線:自定義路由

context.MapRoute(
    "Members_Profile", 
    "Members/Profile/{id}", 
    new { controller = "Profile", action = "Index", id = UrlParameter.Optional }, 
    new string[] { "MyProject.Web.Mvc.Areas.Members.Controllers" } 
    ); 

context.MapRoute(
    "Members_default", 
    "Members/{controller}/{action}/{id}", 
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
    new string[] { "MyProject.Web.Mvc.Areas.Members.Controllers" } 
    ); 

我希望能夠映射以下網址:

~/Members (should map ~/Members/Home/Index) 
~/Members/Profile/3 (should map ~/Members/Profile/Index/3) 

隨着這條路線註冊一切正常。不過,我增加了以下網址:

~/Members/Profile/Add 

,我得到了錯誤:

"The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index(Int32)' in 'MyProject.Web.Mvc.Areas.Members.Controllers.ProfileController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."

我也想有網址

~/Members/Profile/Edit/3 

我應該怎麼纔能有修改所有這些網址正常工作?

回答

3

您將需要添加一些額外的路線,在您已經定義的路線之前。這是因爲這些是您希望在更普通的路線之前選擇的特定路線。

context.MapRoute(
    "Members_Profile", 
    "Members/Profile/Add", 
    new { controller = "Profile", action = "Add" }, 
    new string[] { "MyProject.Web.Mvc.Areas.Members.Controllers" } 
    ); 

context.MapRoute(
    "Members_Profile", 
    "Members/Profile/Edit/{Id}", 
    new { controller = "Profile", action = "Edit", id = UrlParameter.Optional }, 
    new string[] { "MyProject.Web.Mvc.Areas.Members.Controllers" } 
    ); 
+0

我按照你的建議做了一點修改。現在我使用的路線如下(順序很重要): 1.「Members/Profile/Add」 2.「Members/Profile/{id}」 3.「Members/{controller}/{action }/{id}「 我擺脫了Edit的路由,因爲它被最後一個和最通用的路由所覆蓋。感謝您的幫助 – Martin 2010-09-27 09:51:11

+0

很高興爲您提供幫助。請將問題標記爲已回答,以便其他人也可以找到答案。 – Clicktricity 2010-09-27 10:20:26