2011-12-15 97 views
0

我希望有人能幫忙。如何爲Web服務器創建虛擬目錄? (ASP.NET)

我想爲網站創建一個虛擬目錄,並在網絡服務器上指定以下目錄 host/folder1/folder2/default.aspx。

在IIS,這可以通過編輯applicationHost.config文件很容易做到:

<application path="/"> 
<virtualDirectory path="/" physicalPath="%SystemDrive%\inetpub\wwwroot" /> 
</application> 

..但是,當我嘗試同樣的方法,並上傳配置文件到web服務器的根目錄,然後鍵入虛擬路徑,我得到一個404b錯誤。

我不想總是鍵入完整的路徑,而只是想要一些像host/default.aspx一樣短的文件,同時將文件保留在當前目錄中。

這是如何完成的網絡服務器?

希望我的問題有道理。

非常感謝!

回答

0

您需要提供全部站點信息。關鍵信息是端口號,站點名稱和標識本身(刪除任何重複項)。下面的例子我分別提到它爲「MySite」,2,8080。

  <sites> 
       <site name="MySite" id="2"> 
        <application path="/"> 
        <virtualDirectory path="/" physicalPath="%SystemDrive%\inetpub\wwwroot" /> 
        </application> 
        <bindings> 
        <binding protocol="http" bindingInformation="*:8080:" /> 
        </bindings> 
       </site> 

       <applicationDefaults applicationPool="DefaultAppPool" /> 
       <virtualDirectoryDefaults allowSubDirConfig="true" /> 
      </sites> 

同樣可以用C#來完成

  using Microsoft.Web.Administration; 

    using (ServerManager serverManager = new ServerManager()) 
      { 
      Configuration config = serverManager.GetApplicationHostConfiguration(); 
      ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites"); 
      ConfigurationElementCollection sitesCollection = sitesSection.GetCollection(); 

      ConfigurationElement siteElement = sitesCollection.CreateElement("site"); 
      siteElement["name"] = @"MySite"; 
      siteElement["id"] = 2; 
      siteElement["serverAutoStart"] = true; 

      ConfigurationElementCollection bindingsCollection = siteElement.GetCollection("bindings"); 
      ConfigurationElement bindingElement = bindingsCollection.CreateElement("binding"); 
      bindingElement["protocol"] = @"http"; 
      bindingElement["bindingInformation"] = @"*:8080:www.MySite.com"; 
      bindingsCollection.Add(bindingElement); 

      ConfigurationElementCollection siteCollection = siteElement.GetCollection(); 
      ConfigurationElement applicationElement = siteCollection.CreateElement("application"); 
      applicationElement["path"] = @"/"; 
      ConfigurationElementCollection applicationCollection = applicationElement.GetCollection(); 
      ConfigurationElement virtualDirectoryElement = applicationCollection.CreateElement("virtualDirectory"); 
      virtualDirectoryElement["path"] = @"/"; 
      virtualDirectoryElement["physicalPath"] = @"C:\Inetpub\wwwroot"; 
      applicationCollection.Add(virtualDirectoryElement); 
      siteCollection.Add(applicationElement); 
      sitesCollection.Add(siteElement); 

      serverManager.CommitChanges(); 
      }