ASP.NET MVC 5中的自定义文件夹结构 [英] Custom Folder Structure in ASP.NET MVC 5
问题描述
我试图确定是否有可能(或切实可行)在ASP.NET MVC 5中实现鲍勃叔叔的尖叫体系,而不是使用默认的文件夹结构.
I'm trying to determine if it is possible (or practical) to implement Uncle Bob's Screaming Architecture in ASP.NET MVC 5 rather than using the default folder structure.
以下是指向尖叫的体系结构"的描述的链接: http://blog.8thlight.com/uncle-bob/2011/09/30/Screaming-Architecture.html
Here's a link to a description of the Screaming Architecture: http://blog.8thlight.com/uncle-bob/2011/09/30/Screaming-Architecture.html
假设的文件夹结构如下所示:
A hypothetical folder structure would look something like this:
根
- 客户
- 控制器
- CustomerController.cs
- Customers
- Controllers
- CustomerController.cs
- Customer.cs
- Index.cshtml
- Details.cshtml
- Update.cshtml
- 控制器
- EmployeesController.cs
- Controllers
- EmployeesController.cs
- Employee.cs
- Index.cshtml
- Details.cshtml
- Update.cshtml
- 观看次数
- _Layout.cshtml
- Error.cshtml
- Views
- _Layout.cshtml
- Error.cshtml
相应的URL路由如下所示:
The corresponding URL routes would look like this:
- http://www.example.com/customers/ =>客户索引
- http://www.example.com/customers/details/1 = >客户详细信息
- http://www.example.com/customers/update/1 = >客户更新
- http://www.example.com/employees/ =>员工索引
- http://www.example.com/employees/details/1 = >员工详细信息
- http://www.example.com/employees/update/1 = >员工更新
- http://www.example.com/customers/ => Customer Index
- http://www.example.com/customers/details/1 => Customer Details
- http://www.example.com/customers/update/1 => Customer Update
- http://www.example.com/employees/ => Employee Index
- http://www.example.com/employees/details/1 => Employee Details
- http://www.example.com/employees/update/1 => Employee Update
我创建了一个自定义的RazorViewEngine,并添加了适当的视图位置格式(例如〜/{1}/Views/{0} .cshtml")和部分视图位置格式(例如〜/Shared/Views/{ 0} .cshtml").我还将共享的_ViewStart.cshtml移到了根目录,并将Views/Shared文件夹的web.config与根级别的web.config合并,以避免必须在所有View文件夹中复制这两个文件.
I've created a custom RazorViewEngine and added the appropriate view location formats (e.g. "~/{1}/Views/{0}.cshtml") and partial view location formats (e.g. "~/Shared/Views/{0}.cshtml"). I've also moved the shared _ViewStart.cshtml to the root and merged the Views/Shared folder's web.config with the root-level web.config to avoid having to duplicate these two files in all of the View folders.
但是,如果我尝试导航到索引页面(例如 http://www.example .com/employees/),我收到403.14错误(禁止).所有其他路由(包括 http://www.example.com/employees/index )都可以正常工作很好.
Everything works great, however, if I try navigating to an index page (e.g. http://www.example.com/employees/) I get a 403.14 Error (Forbidden). All other routes (including http://www.example.com/employees/index) work just fine.
我的猜测是IIS明确阻止了到控制器的index方法的路由,因为URL与文件系统中的文件夹重合并且默认情况下禁用了目录浏览.但是,如果启用目录浏览,它实际上会将我带到实际的目录列表,而不是路由到控制器的index方法.
My guess is that IIS is explicitly blocking the route to the controller's index method because the URL coincides with a folder in the filesystem and directory browsing is disabled by default. If I enable directory browsing, however, it actually takes me to the actual directory listing rather than routing to the controller's index method.
我可以将客户和雇员"文件夹移动到子文件夹中(即,将它们移出根目录),并且一切正常,但我想尝试将这些文件夹保持在顶层(根据《尖叫建筑指南》)
I can move the Customers and Employees folders into a subfolder (i.e. move them out of the root) and everything works fine, but I'd like to try to keep these folders at the top level (per the Screaming Architecture guidelines).
有人对此问题有解决方案吗?
Does anyone have a solution for this issue?
请注意,MVC Areas不是我正在寻找的解决方案,因为它不允许上述文件夹结构(即以高级用例命名的顶级文件夹和直接包含在Views文件夹中的视图)而不是子文件夹中的内容.
Please note that MVC Areas is not the solution I'm looking for as it does not allow for the folder structure described above (i.e. top-level folders named after high-level use cases and views contained directly within the Views folder rather than in a subfolder).
推荐答案
我敢打赌,您对IIS的看法是正确的.如果您有两个路径映射到同一资源,则首先在IIS端检查物理路径.
I'm betting you are right about IIS then. If you have two paths mapped to the same resource, the physical path is checked first on the IIS side.
I was digging around the routes configuration and found the property RouteExistingFiles on
RouteCollection
and think this could work.我将值设置为
true
,并使用项目中的空文件夹在本地进行测试,路由重定向到Home/Index
,然后导航到localhost:xxx/MyFolder
.它正常工作.I set the value to
true
and tested locally with an empty folder in the project, a route redirecting toHome/Index
, and navigating tolocalhost:xxx/MyFolder
. It worked correctly.因此,您所需要做的就是将此属性设置为true,以便它首先选择Asp.net路由而不是物理路由.
So then all you should need to do is set this property to true for it to choose Asp.net routes first instead of physical routes.
这篇关于ASP.NET MVC 5中的自定义文件夹结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Controllers
- 控制器