【发布时间】:2014-03-17 11:21:07
【问题描述】:
我在尝试为我的 MVC 项目建立非传统文件夹结构时遇到了一个奇怪的问题:
\Franchises\Compliance\FeesPaid\FeesPaidController
\Franchises\Compliance\FeesPaid\Views\..
\Franchises\Compliance\FeesPaid\ViewModels\..
我的计划是在每个子文件夹中放置 1 个控制器,以及关联的视图/视图模型文件夹。我不希望有一个只包含一个类的 /Controllers/ 文件夹。
出于测试目的,我像这样手动路由:
routes.MapRoute("whocares", "Franchises/Compliance/{controller}/{action}/{id}",
new { controller = "FeesPaid",
action = "Index",
id = UrlParameter.Optional });
我遇到了神秘的行为。这些 URL 路由正确:
http://localhost:12345/Franchises/Compliance/FeesPaid/Index
http://localhost:12345/Franchises/Compliance/FeesPaid/Index/123
但此 URL 失败并出现“Web 服务器配置为不列出此目录的内容”错误:
http://localhost:12345/Franchises/Compliance/FeesPaid
我发现只有当控制器名称与文件夹名称匹配时才会出现这种情况(减去“*Controller”)。因此,将控制器名称伪造为其他名称可以使其工作:
\Franchises\Compliance\FeesPaid\FeesPaidXController
http://localhost:12345/Franchises/Compliance/FeesPaidX
http://localhost:12345/Franchises/Compliance/FeesPaidX/Index
http://localhost:12345/Franchises/Compliance/FeesPaidX/Index/123
当 URL 依赖于默认操作和 id 时,为什么具有匹配名称的控制器和文件夹会导致它失败?
编辑
经过调查发现 IIS 将请求路由到 DirectoryListingModule 而不是 MVC,因此它甚至没有到达 MVC 路由。
我尝试了以下方法无济于事:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" > <--- attribute added
<remove name="FormsAuthenticationModule"/>
</modules>
</system.webServer>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" >
<remove name="FormsAuthenticationModule"/>
<remove name="DirectoryListingModule"/> <--- Fails to start web app with "lock violation"
</modules>
</system.webServer>
如何强制 IIS 将这些请求路由到 MVC 而不是 DirectoryListingModule?如果没有 IIS 配置更改,我可能无法在生产中控制。
【问题讨论】:
-
请注意,包含控制器的目录与 MapRoutes 没有任何关系。例如,您的控制器可能都在项目的根目录中,只要 Request Url 与 MapRoute 匹配,控制器就会执行。
-
如果你浏览到
http://localhost:12345/Franchises/Compliance/FeesPaid/会发生什么(注意额外的斜线)。 -
末尾的额外斜线没有任何区别。不知何故,MVC 缺少路由,请求将转到某个通用目录列表模块。
-
它可能只是一个 ASP.Net 功能,在您的 web.config 中,如果启用(设置为 true):
<system.webServer><modules runAllManagedModulesForAllRequests="true">会怎样?我敢打赌它有效。 -
好像和这个有关,我正在进一步调查:forums.asp.net/t/…
标签: c# asp.net-mvc-routing asp.net-mvc-5 asp.net-mvc-controller