asp.net-mvc - Asp.Net MVC5,构建大型项目。地区?

标签 asp.net-mvc

我有 Asp.Net 背景,正在为一个新项目评估 Asp.Net MVC。我不知道如何充分构建一个大型项目。

我对模型/ View / Controller 架构很满意,目前我正在尝试让区域发挥作用(这看起来相当复杂)。

区域内可以有区域吗? 可以将 View 放入 dll 中吗?

我真的需要一个起点,是否有资源展示如何构建大型 MVC 项目,假设项目中最终会有 100 多个 View ,我不希望它们全部位于同一个文件夹中,理想情况下我想要子文件夹

感谢您的帮助

编辑: 我可以看到每个 Controller 映射到一个 View 文件夹,我想要的是更像这样的东西

 Areas
  Mail
    Absence
      SimpleAbsenceController.cs
      ComplexAbsenceController.cs
    Overtime
      SimpleOvertimeController.cs
      ComplexOvertimeController.cs
    Etc

Edit2:也许这更多的是一个路由问题,我可以从以下位置映射: http://www.mystuff.com/SimpleAbsence/Index

到邮件/缺勤/SimpleAbsenceController

从根本上来说,我想要一种将项目组织到文件夹中的方法

最佳答案

我讨厌回答自己的问题,因为我觉得这会破坏其他试图提供帮助的人的工作,同时其他人可能稍后会看到这个问题,而这些确实是新手问题,所以......

(这全部来自于阅读Pro ASP.Net MVC5,好书)

我想知道 Asp.Net MVC 在使用子文件夹或 dll 方面有多大的灵 active 。此答案解决了子文件夹问题。

答案

  1. 您可以将 Controller 放在磁盘上任何您喜欢的文件夹中,这些都是经过编译的,但是...

  2. 当您使用区域时,AreaRegistration.cs 文件中的 MapRoute 会自动限制 到该区域的命名空间的路由。因此,如果您将 Controller 移动到您必须更改命名空间或 Url.Action 等方法的区域,将会失败

  3. View 必须保持在原来的位置,因此对于名为 Fred 的 Controller ,必须具有以下结构:

View
 - Fred
  - Action1 
  - Action2
  - etc

You can work around all of this using your own routing system, you could probably work around the namespace issue with a custom route, but my view is as a newbie you should work with the system until you know enough to fully understand the consequences of breaking the rules

So this means you could have a large project with a few hundred Views all lumped together in one folder. It's not quite as bad as it seems as the controllers can be in sub folders and you can directly map from them to the Views.

You also have flexibility in the routing system, so regardless where controller are on disk you can have any urls you like!

e.g. this route maps from
http://www.example.com/App/DoSomething to
http://www.example.com/Home/Something with no changes necessary

routes.MapRoute("NewRoute", "App/Do{action}", new { controller = "Home" });

注意,如果您这样做,请确保使用 Html.ActionLink 或 Url.Action(或等效项),而不是直接链接,它们足够聪明,可以根据路由生成正确的 URL。

正如我所说,完全是新手问题,但我相信其他人也会有同样的问题,感谢 MiniRagnarok 提供的现实生活项目示例

关于asp.net-mvc - Asp.Net MVC5,构建大型项目。地区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23246115/

相关文章:

asp.net-mvc - 依赖注入(inject)存储库生命周期

asp.net-mvc - 在 Automapper 中修剪字符串

.net - 如何在 MVC3 Controller 中处理存储库对象

c# - Entity Framework 迁移未检测到任何更改

javascript - 使用多个参数将数据从 javascript 传递到 MVC Controller

asp.net-mvc - 使用 MVC 和 jQuery 进行内联客户端验证

c# - 使用 MEF 从 X 个文件夹导入零件

c# - 没有 MVC 的 .Net Core 2 路由

c# - 如何在另一个类中获取当前 Controller 实例?

c# - MVC Controller : Using LINQ to check for duplicate value already existing in table before Save?