How to disable or reprioritize IIS DirectoryListingModule under MVC module?

半城伤御伤魂 提交于 2019-11-29 10:54:05

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.

I think if you want to separate your controllers in folders, so they are not under the "controllers", you should use "areas" feature that MVC provides. It is designed for this purpose.

I've noticed that if I have a custom http module, pointed to from web.config like

    <modules>
        <remove name="WebDAVModule" />
        <add name="CustomHttpModule" type="MyCompany.Types.CustomHttpModule" preCondition="managedHandler" />
    </modules>

Then I can get code to run before DirectoryListingModule hijacks the process, but I haven't found out what to do about it when I've detected it's about to hit a physical folder.

using System.IO;
using System.Web;
using System.Linq;
using System.Linq.Expressions;

namespace MyCompany.Types
{
public class CustomHttpModule : IHttpModule
{
    public void OnAcquireRequestState(object sender, EventArgs ea)
    {
        List<string> physicalFolders = Directory.EnumerateDirectories(AppContext.BaseDirectory).Select(f => f.Substring(1 + f.LastIndexOf('\\'))).ToList();
        string projectBase = /* get from web.config */.TrimStart('/');

        string possiblePhysicalFolder = application.Request.Url.AbsolutePath.TrimStart('/').Replace(projectBase, "").TrimStart('/');
        if (physicalFolders.Exists(f => possiblePhysicalFolder.StartsWith(f)))
            /* what to do??? */;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!