[英]How can I model this route in ASP.NET MVC 3?
I need to model routes that act a little strange to me. 我需要模拟对我来说有点奇怪的路线。 Here is what they would like me to do:
以下是他们希望我做的事情:
/AssetManagement -> Asset Landing page
/AssetManagement/Add -> Add an asset
/AssetManagement/Edit -> Edit an asset
/AssetManagement/Locations -> Locations landing page
/AssetManagement/Locations/Add -> Add a location
/AssetManagement/Locations/Edit -> Edit a location
I'm not sure, but I think this needs to be modeled with two controllers. 我不确定,但我认为这需要用两个控制器建模。 AssetsController and LocationsController.
AssetsController和LocationsController。 I think the views Add/Edit/Index would exist under the respected View folders and I would probably have two routes defined:
我认为视图添加/编辑/索引将存在于受尊重的视图文件夹下,我可能会定义两个路径:
routes.MapRoute(
"AssetManagement", // Route name
"AssetManagement/{action}/{id}", // URL with parameters
new { controller = "Assets", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new[] { "MyApplication.Web.Website.Controllers" }
);
routes.MapRoute(
"AssetManagementLocations", // Route name
"AssetManagement/{controller}/{action}/{id}", // URL with parameters
new { controller = "Locations", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new[] { "MyApplication.Web.Website.Controllers" }
);
It just feels a bit wrong to me. 这对我来说感觉有点不对劲。 Am I missing a best practice on handling stuff?
我错过了处理东西的最佳做法吗? What kinds of problems could this cause down the road?
这会导致什么样的问题? A lot of the system they want me to build works like this.
他们希望我构建的很多系统都是这样的。
// Edit If this is a bad place to ask questions like this, where should I ask them? //编辑如果这是一个提出这样问题的好地方,我应该在哪里问他们?
What you can do is create a Area. 你可以做的是创建一个区域。
In the area create 2 controllers one for asset and other for Location. 在该区域中创建2个控制器,一个用于资产,另一个用于位置。
Instead of playing with routes(which can be dangerous) play with the MVC concept. 而不是使用路线(可能是危险的)玩MVC概念。
The 2nd Route you have specified closely relates to Areas. 您指定的第二条路线与区域密切相关。 When you create the area the route is automatically created for you.
创建区域时,将自动为您创建路径。 hope this helps.
希望这可以帮助。
Area - AssetManagement
Controller1 - HomeController
Action1 - Add
Action2 - Delete
Controller2 - LocationsController
Action1 - Add
Action2 - Delete
routes.MapRoute(
"AssetManagementLocations", // Route name
"{Area}/{controller}/{action}/{id}", // URL with parameters
new { controller = "Locations", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new[] { "MyApplication.Web.Website.Controllers" }
);
Check out the attributerouting project. 查看attributerouting项目。 You can Nuget install into mvc and decorate your action methods or controllers with the path- this leads to clear intention of exactly which routes map to which controller/method
您可以将Nuget安装到mvc并使用路径装饰您的操作方法或控制器 - 这导致明确意图确切地将哪些路由映射到哪个控制器/方法
https://github.com/mccalltd/AttributeRouting/wiki/2.-Usage https://github.com/mccalltd/AttributeRouting/wiki/2.-Usage
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.