简体   繁体   English

如何在ASP.NET MVC 3中建模此路由?

[英]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.

相关问题 如何进行此路由(ASP.Net MVC2)? - How can I make this Route (ASP.Net MVC2)? 如何在 ASP.NET MVC 中进行路由配置? - How can I do route configuration in ASP.NET MVC? ASP.NET MVC 5-如何更改httpErrors中的路由? - ASP.NET MVC 5 - How can I change the route in httpErrors? 如何使用ASP.NET MVC设置此路由? - How do I set up this route with ASP.NET MVC? 如何根据ASP.NET VNEXT MVC6中给出的路径进行虚拟路由/重定向? - How can I do a virtual route/redirect based on the path given in ASP.NET VNEXT MVC6? 如何在 ASP.NET MVC Core 中执行多路由约束? - How can I do a multiple route constraints in ASP.NET MVC Core? 如何将请求路由到页面控制器以获取ASP.NET MVC 4中不存在的路由 - How can I route requests to a page controller for routes that don't exists in ASP.NET MVC 4 如何将特定的子域请求重新路由到其他ASP.NET MVC控制器? - How can I re-route a specific subdomain request to a different ASP.NET MVC controller? 我如何将现有模型(设备)数据附加到MVC ASP.NET中的另一个模型(用户)数据 - How can i attach existing model(device) data to another model(user) data in mvc asp.net 我可以在asp.net MVC视图中采用模型属性吗? - can i adopt model property in the view asp.net MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM