asp.net-mvc - 如何在 MVC 模块下禁用或重新调整 IIS DirectoryListingModule 的优先级?

标签 asp.net-mvc iis-7 httpmodule directory-listing

我使用 MVC 文件夹结构,其中 URL 路由恰好与目录名称匹配,例如:

<proj>\My\Cool\Thing\ThingController.cs

需要通过此网址访问:

http://blahblah/My/Cool/Thing

我的 MVC 路由工作正常,但不幸的是,当依赖默认的 {action} 和 {id} 时,IIS Express 会将请求路由到 DirectoryListingModule,因为它直接匹配文件夹名称。目录列表当然被禁用,所以我得到:

The Web server is configured to not list the contents of this directory.
Module     DirectoryListingModule
Notification       ExecuteRequestHandler
Handler    StaticFile

为了解决这个问题,我已经尝试过:

1. runAllManagedModulesForAllRequests = true

<system.webServer>
<modules runAllManagedModulesForAllRequests="true" >   
//Makes no difference

2. Removing module

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true" >
    <remove name="DirectoryListingModule"/>   
    // Won't let me as module is locked in IIS
  </modules>
</system.webServer>

3. Removing lock & module

// applicationhost.config
<add name="DirectoryListingModule" lockItem="false" />

// web.config
<remove name="DirectoryListingModule"/>
// Causes startup error"Handler "StaticFile" has a bad module "DirectoryListingModule" in its module list"


4. Removing lock & removing/readding module (to change order) - makes no difference

// web.config
<remove name="DirectoryListingModule"/>
<add name="DirectoryListingModule"/>

把我的头发扯下来。如何让 IIS 将其路由到我的 MVC 应用程序而不是 DirectoryListingModue?最好是 web.config 中的解决方案,这样我们就不需要在生产中重新配置 IIS。

(一种解决方法是保留我的文件夹结构,但将其全部存储在/Areas/... 下,只是为了打破文件夹路径和 url 之间的匹配。这是一个可怕的黑客和最后的手段。)

编辑以添加路线映射

我正在创建与每个 Controller 的命名空间相关的自定义路由(命名空间始终与文件夹匹配)。请注意,目前所有内容都放在“Modules”命名空间/文件夹下,只是为了避免上述问题。

    private static void RegisterAllControllers(RouteCollection routes)
    {
        const string controllerSuffix = "Controller";
        const string namespacePrefix = "My.Cool.Websire.UI.Modules.";

        var controllerTypes = Assembly.GetExecutingAssembly().GetTypes().Where(x => x.IsSubclassOf(typeof(Controller))).ToList();

        foreach (var controllerType in controllerTypes)
        {
            // Turn My.Cool.Website.UI.Modules.X.Y.Z.Abc.AbcController into a route for url /X/Y/Z/Abc/{action}/{id}
            var fullNamespace = controllerType.Namespace ?? "";

            var relativeNamespace = fullNamespace.Substring(namespacePrefix.Length, fullNamespace.Length - namespacePrefix.Length);

            var controllerName =
                controllerType.Name.EndsWith(controllerSuffix)
                ? controllerType.Name.Substring(0, controllerType.Name.Length - controllerSuffix.Length)
                : controllerType.Name;

            var url = relativeNamespace.Replace(".", "/") + "/{action}/{id}";

            var routeName = "Dedicated " + controllerName + " route";

            routes.MapRoute(routeName, url, new { controller = controllerName, action = "Index", id = UrlParameter.Optional });
        }
    }

最佳答案

我现阶段的解决方案是将WebUI项目的MVC内容放在/Modules/文件夹下:

My.Cool.Site.WebUI/Modules/Something/Blah/BlahController
My.Cool.Site.WebUI/Modules/Something/Blah/Views/...
My.Cool.Site.WebUI/Modules/Something/Blah/PartialViews/...

然后使用发布的路由代码我可以通过 url 访问它:

http://.../Something/Blah/[action]

因为文件位于/Modules/文件夹下,所以这会破坏 URL 和文件夹路径之间的匹配,从而解决我的问题。

不是一个很好的解决方案,但可以完成工作。

关于asp.net-mvc - 如何在 MVC 模块下禁用或重新调整 IIS DirectoryListingModule 的优先级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21842206/

相关文章:

Html.TextBox 控件的 jQuery UI Datepicker

c# - 有没有比通过 ASP.NET MVC 中的 ViewModel 使用多个模型更好的方法?

javascript - Controller 操作触发后将文件保存到本地磁盘,ASP.NET MVC

iis-7 - IIS 7和php大文件上传问题

.net - NetTcpActivator 服务(Net.Tcp Listener Adapter)偶尔停止响应

c# - 如何调试崩溃的转储文件?

visual-studio-2012 - 如何注册适合VS2012 Cassini和IIS7的HttpModule?

asp.net-mvc - aspnet-api-versioning 有什么好处?

asp.net - 非 aspx 页面不会触发 IHttpModule 错误事件

c# - 如何在 IHttpModule 中以 404 结束请求?