ASP.NET 对此一无所知。您需要查询 IIS 以获取默认文档列表。
这样做的原因是 IIS 将在您的 web 文件夹中查找 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 环境(例如共享主机)中,这将不起作用。