ASP.Net MVC路由问题

时间:2011-03-29 13:31:42

标签: asp.net-mvc-3 routing asp.net-mvc-routing

我有一个名为MyArea的区域,它的注册方式如下:

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

我有一个Unit控制器,其动作叫做Unit。请求帮助,我缺少什么?

谢谢!

1 个答案:

答案 0 :(得分:1)

你说“我有一个单位控制器,其行动称为单位。请帮助,我缺少什么?”

目前您的路线映射......

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

在路线注册中。否则,应映射到第二条路线的东西将被第一条路线拦截。