Bre*_*ill 5 asp.net-mvc iis-7 directory-listing httpmodule
我使用MVC文件夹结构,其中URL路由恰好与目录名称匹配,例如:
<proj>\My\Cool\Thing\ThingController.cs
Run Code Online (Sandbox Code Playgroud)
需要通过以下网址访问:
http://blahblah/My/Cool/Thing
Run Code Online (Sandbox Code Playgroud)
我可以使用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
Run Code Online (Sandbox Code Playgroud)
为了解决这个问题,我已经尝试过:
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"/>
Run Code Online (Sandbox Code Playgroud)
扯掉我的头发。如何获得IIS将其路由到我的MVC应用程序而不是DirectoryListingModue?最好是web.config中的解决方案,因此我们不需要在生产中重新配置IIS。
(一种解决方法是保留我的文件夹结构,但将其全部存储在/ Areas / ...下,只是为了打破文件夹路径和url之间的匹配。这是一个糟糕的方法,也是最后的手段。)
编辑以添加路线图
我正在创建相对于每个控制器的名称空间的自定义路由(名称空间始终与文件夹匹配)。请注意,所有内容都放在当前的“模块”名称空间/文件夹下,只是为了避免上述问题。
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 });
}
}
Run Code Online (Sandbox Code Playgroud)
我现阶段的解决方案是将 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/...
Run Code Online (Sandbox Code Playgroud)
然后使用发布的路由代码我可以通过 url 访问它:
http://.../Something/Blah/[action]
Run Code Online (Sandbox Code Playgroud)
因为这些文件位于 /Modules/ 文件夹下,所以这会破坏 URL 和文件夹路径之间的匹配,从而解决我的问题。
这不是一个很好的解决方案,但可以完成工作。
| 归档时间: |
|
| 查看次数: |
2632 次 |
| 最近记录: |