Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
639 views
in Technique[技术] by (71.8m points)

asp.net mvc - How to disable or reprioritize IIS DirectoryListingModule under MVC module?

I use an MVC folder structure where the URL routes happen to match the directory names, eg.:

<proj>MyCoolThingThingController.cs

Needs to be accessible by this url:

http://blahblah/My/Cool/Thing

I have the MVC routing working but unfortunately when relying on default {action} & {id}, IIS Express is routing the request to DirectoryListingModule instead, since it directly matches a folder name. Directory listing is disabled of course so instead I get:

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

To fix this I have already tried:

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"/>

Tearing my hair out. How can I get IIS to route this to my MVC app instead of DirectoryListingModue?? Preferably a solution in web.config so we don't need to reconfigure IIS in production.

(One workaround is to keep my folder structure but store it all under /Areas/... instead, just to break the match between folder path & url. This is a terrible hack & last resort.)

edit to add route mapping

I am creating custom routes relative to each controller's namespaces (namespaces always match folders). Note that everything is put under the "Modules" namespace / folder currently just to avoid the problem described above.

    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 });
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

My solution at this stage is to place the MVC contents of the WebUI project under a /Modules/ folder:

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

Then using the route code posted I can access this via url:

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

Because the files are under /Modules/ folder, this breaks the match between the URL and the folder path which gets around my problem.

Not a great solution but does the job.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...