views:

1449

answers:

2

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

+5  A: 

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).

Kev
Kev, thanks for quick and thorough answer!
No problem. I live and breath this stuff day in day out. :)
Kev
Be careful, because of this line: new DirectoryEntry("IIS://Localhost/W3SVC/1/root") as this will only work when the application is installed on the default web site (the 1 refers to the number attribuate to the Default Web Site). If the application is installed on another Web Site, that number will be different.
joerage
@joerage - obviously, but it's an example of how to do this....that said I've made this more clear in the answer.
Kev
A: 

Server Error in '/' Application.

Runtime Error Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.

Details: To enable the details of this specific error message to be viewable on remote machines, please create a tag within a "web.config" configuration file located in the root directory of the current web application. This tag should then have its "mode" attribute set to "Off".

Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's configuration tag to point to a custom error page URL.

Ramu