1

プロジェクトの将来性をさらに高めるために、C# を使用して Web ディレクトリ内のインデックス/デフォルト ページのフル パスとファイル名を取得する最良の方法を見つけようとしています。

「Server.MapPath("/test/")」は「C:\www\test\」を提供します

...そうです: 'Server.MapPath(Page.ResolveUrl("/test/"))'

...しかし、「C:\www\test\index.html」が必要です。

誰かがそのディレクトリを参照したときに Web サーバーが提供するファイル名を取得する既存の方法を知っている人はいますか?

助けてくれてありがとう、飼料

4

1 に答える 1

5

ASP.NET はこれを認識していません。IIS にクエリを実行して、既定のドキュメント リストを取得する必要があります。

これは、IIS が Web フォルダで IIS の既定のドキュメント リストにある最初に一致するファイルを検索し、スクリプト マッピングでそのファイル タイプに一致する ISAPI 拡張子に (拡張子で) 渡すためです。

既定のドキュメント リストを取得するには、次の手順を実行します (IIS 番号 = 1 の例として既定の Web サイトを使用)。

using System;
using System.DirectoryServices;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (DirectoryEntry w3svc =
                 new DirectoryEntry("IIS://Localhost/W3SVC/1/root"))
            {
                string[] defaultDocs =
                    w3svc.Properties["DefaultDoc"].Value.ToString().Split(',');

            }
        }
    }
}

次に、配列を反復defaultDocsしてフォルダーに存在するファイルを確認するケースになります。最初に一致したものがデフォルトのドキュメントです。例えば:

// Call me using: string doc = GetDefaultDocument("/");
public string GetDefaultDocument(string serverPath)
{

    using (DirectoryEntry w3svc =
         new DirectoryEntry("IIS://Localhost/W3SVC/1/root"))
    {
        string[] defaultDocs =
            w3svc.Properties["DefaultDoc"].Value.ToString().Split(',');

        string path = Server.MapPath(serverPath);

        foreach (string docName in defaultDocs)
        {
            if(File.Exists(Path.Combine(path, docName)))
            {
                Console.WriteLine("Default Doc is: " + docName);
                return docName;
            }
        }
        // No matching default document found
        return null;
    }
}

残念ながら、部分信頼の ASP.NET 環境 (共有ホスティングなど) を使用している場合、これは機能しません。

于 2009-06-24T15:26:52.257 に答える