ASP.Net MVC 路由问题

发布于 2024-10-27 21:12:11 字数 913 浏览 5 评论 0原文

我有一个名为 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 });

一个属性有多个单元应该可以工作,所以我希望我的 url 看起来像这样:

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

我用于 Html.ActionLink 的代码如下所示:

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

我有一个单位控制器,其操作称为单位。请帮忙,我缺少什么?

谢谢!!

I have an area called MyArea and it's registered like so:

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

It should work that one property has many units, so I would like my url to look something like this:

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

The code i use for the Html.ActionLink looks like:

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

I have an Unit controller with an action called Unit. Pleas help, what am i missing?

Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北笙凉宸 2024-11-03 21:12:11

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

并且您的路由映射当前是...

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

您希望 MVC 如何知道该路由使用哪个控制器?您需要指定 controller = "Unit"

Update

在您的路由注册中切换顺序

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

。否则,应该映射到第二条路线的内容将被第一条路线拦截。

You say "I have an Unit controller with an action called Unit. Pleas help, what am i missing?"

and your route mapping is currently ...

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

How would you expect MVC to know what controller to use for that route? You need to specify controller = "Unit"

Update

Switch the order of

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

in your route registration. Otherwise, something that should map to the second route will be intercepted by the first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文