2011-03-29 91 views
1

我有一個名爲區域MyArea和它的登記,像這樣:ASP.Net MVC路線問題

context.MapRoute(null, "MyArea", new { controller = "MyAreaController", action = "Index" }); 

//Properties 
context.MapRoute(null, "MyArea/properties", new { controller = "Property", action = "Index" }); 
context.MapRoute(null, "MyArea/properties/edit/{propertyId}", new { controller = "Property", action = "Property" }); 

//Units 
context.MapRoute(null, "MyArea/properties/edit/{propertyId}/units/{unitId}", new { action = "Unit", propertyId = 1, unitId = 1 }); 

它應該是一個酒店有很多單位,所以我想我的網址看起來是這樣的:

http://localhost:50182/myarea/properties/edit/4/units/1

我使用的Html.ActionLink的代碼如下所示:

@Html.ActionLink("Add new Unit", "Unit", "Unit", new { propertyId = 1, unitId = 1 }) 

我有一個單位控制器與一個稱爲單位的行動。請幫助,我錯過了什麼?

謝謝!

+0

你面臨什麼問題? – 2011-03-29 13:46:59

+0

鏈接轉到http:// localhost:50182/myarea/properties/edit/4/units?controller = Property&Length = 4 而不是: http:// localhost:50182/myarea/properties/edit/4/units/1 我收到以下錯誤消息: 值不能爲空或爲空。 參數名稱:controllerName – Pete 2011-03-29 13:50:19

回答

1

你說:「我有一個單位控制器,帶有一個名爲Unit的動作。請幫忙,我錯過了什麼?」

和你的路由映射是目前...

context.MapRoute(null, "MyArea/properties/edit/{propertyId}/units/{unitId}", new { action = "Unit", propertyId = 1, unitId = 1 }); 

你怎麼會期望MVC知道使用什麼控制器,這條路線?您需要指定controller = "Unit"

更新

開關在你的路線登記

context.MapRoute(null, "MyArea/properties/edit/{propertyId}", new { controller = "Property", action = "Property" }); 

    //Units 
    context.MapRoute(null, "MyArea/properties/edit/{propertyId}/units/{unitId}", new { action = "Unit", propertyId = 1, unitId = 1 }); 

的順序。否則,應該映射到第二條路線的東西將被第一條路線攔截。

+0

我確實嘗試過,但網址是: http:// localhost:50182/myarea/properties/edit/ – Pete 2011-03-29 13:59:43

+0

檢查更新。 – smartcaveman 2011-03-29 14:07:48

+0

把它翻了個遍,但是這個URL到localhost:50182/myarea/properties /編輯 – Pete 2011-03-29 14:21:55