C#/。NET Server默认/索引页面的路径 [英] C#/.NET Server Path to default/index page

查看:120
本文介绍了C#/。NET Server默认/索引页面的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我尝试进一步面向未来的项目时,我试图找到使用C#检索Web目录中索引/默认页面的完整路径和文件名的最佳方法,而不知道Web服务器的文件名列表可能性。



'Server.MapPath(/ test /)'给我'C:\\\\''



......所以:'Server.MapPath(Page.ResolveUrl(/ test /))'



...但是我需要'C:\\\ n \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\有人浏览到该目录 - 是default.aspx,还是index.html,还是其他什么?



感谢您的帮助,
饲料

解决方案

ASP.NET不知道这一点。您需要在IIS中查询默认文档列表。



原因是IIS将在您的Web文件夹中查找IIS默认文档中的第一个匹配文件然后在脚本映射中将列表移交给该文件类型(通过扩展名)的匹配ISAPI扩展名。



要获取默认文档列表,您可以执行以下操作(使用默认网站作为IIS编号= 1)的示例:

 使用System; 
使用System.DirectoryServices;

命名空间ConsoleApplication1
{
class程序
{
static void Main(string [] args)
{
using( DirectoryEntry w3svc =
new DirectoryEntry(IIS:// Localhost / W3SVC / 1 / root))
{
string [] defaultDocs =
w3svc.Properties [DefaultDoc ] .Value.ToString()分割( '')。

}
}
}
}

然后迭代 defaultDocs 数组以查看文件夹中存在哪个文件,第一个匹配是默认文档。例如:

  //使用以下方法给我打电话: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(defaultDocs中的字符串docName)
{
if(File.Exists(Path.Combine(path,docName)))
{
Console .WriteLine(默认文件是:+ docName);
返回docName;
}
}
//找不到匹配的默认文件
返回null;
}
}

可悲的是,如果你在这里,这将不起作用部分信任ASP.NET环境(例如共享主机)。


In my attempt to further future-proof a project I am trying to find the best way to retrieve the full path and filename of the index/default page in a web directory using C# and without knowing the web server's list of filename possibilities.

'Server.MapPath("/test/")' gives me 'C:\www\test\'

...so does: 'Server.MapPath(Page.ResolveUrl("/test/"))'

...but I need 'C:\www\test\index.html'.

Does anyone know of an existing method of retrieving the filename that the webserver will serve up when someone browses to that directory - be it default.aspx, or index.html, or whatever?

Thanks for any help, fodder

解决方案

ASP.NET has no knowledge of this. You would need to query IIS for the default document list.

The reason for this is that IIS will look in your web folder for the first matching file in the IIS default document list then hand off to the matching ISAPI extension for that file type (by extension) in the script mappings.

To obtain the default document list you can do the following (using the Default Website as an example where the IIS Number = 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(',');

            }
        }
    }
}

It would then be a case of iterating the defaultDocs array to see which file exists in the folder, the first match is the default document. For example:

// 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;
    }
}

Sadly this won't work if you're in a partial trust ASP.NET environment (for example shared hosting).

这篇关于C#/。NET Server默认/索引页面的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆