Web サイトの特定のセクションでディレクトリ ブラウジングを使用していますが、ユーザーはデフォルトの ASP.NET をあまり好みません。
ディレクトリブラウジング。正直なところ、私たちも特に気にしません。
mvolo のカスタム ディレクトリ ブラウジング モジュールに出会い、それを使用しようとしました。ただし、ルートの web.config で有効にすると、(ご想像のとおり) デフォルト ページなしですべてのフォルダーのディレクトリ参照が可能になることがわかりました。ルートで enabled="false" を設定すると、一般的なエラー ページによってキャッチされる HttpException がスローされますが、要求されたページに読み込み中に要求する追加の画像がある場合など、すべての要求で例外が発生します。
私が信じているように (そして私が間違っている可能性もあります)、デフォルトのディレクトリ参照モジュールは、デフォルトのフォルダーがなく、特定のファイルを要求していない場合にのみ、有効な属性をチェックします (たとえば、mysite.com/images/ と mysite. com/images/logo.gif)。
カスタム モジュールの機能を再構築しましたが、すべての要求に対してではなく、有効にした場合にディレクトリの参照が必要な状況でのみモジュールを完全に実行するように制限する方法がわかりません。モジュールのコードの一部を次に示します。
public void Init(HttpApplication app)
{
app.PreRequestHandlerExecute += new EventHandler(this.OnPreRequestHandlerExecute);
}
public void OnPreRequestHandlerExecute(object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
config = (DirectoryListingConfigSection)WebConfigurationManager.GetSection("directoryBrowsing", context.Request.Path);
if (this.config == null)
{
throw new Exception("Missing <directoryBrowsing> configuration section.");
}
/* I only want to check this if it's necessary, not for things
like mysite.com/images/logo.gif or mysite.com/about/history.aspx
-- those shouldn't give a 403 error */
if (!config.Enabled)
{
context.Response.Status = "403 Forbidden";
}
/* The rest of the code goes below, and should only process
if Directory Browsing is necessary and enabled */
}