문제

프로젝트를 더욱 발전 시키려고 시도하면서 C#을 사용하여 웹 디렉토리에서 인덱스/기본 페이지의 전체 경로와 파일 이름을 검색하고 웹 서버의 파일 이름 가능성 목록을 모르는 가장 좋은 방법을 찾으려고 노력하고 있습니다.

'server.mappath ( "/test/")' 'c : www test '

... 'server.mappath (page.resolveurl ( "/test/"))' '

...하지만 'c : www test index.html'이 필요합니다.

누군가가 해당 디렉토리를 탐색 할 때 Webserver가 제공 할 파일 이름을 검색하는 기존 방법을 알고 있습니까?

도와 주셔서 감사합니다. 사료

도움이 되었습니까?

해결책

ASP.NET은 이것에 대해 알지 못합니다. 기본 문서 목록에 대한 IIS를 쿼리해야합니다.

그 이유는 IIS가 IIS 기본 문서 목록의 첫 번째 일치 파일에 대한 웹 폴더를보고 스크립트 매핑에서 해당 파일 유형 (확장자 별)에 대한 일치하는 ISAPI 확장자로 나누기 때문입니다.

기본 문서 목록을 얻으려면 다음을 수행 할 수 있습니다 (기본 웹 사이트를 IIS 번호 = 1 예로 사용) :

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 환경 (예 : 공유 호스팅)에 있으면 작동하지 않습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top