2012-12-31 95 views
1

我有一個MVC應用程序並部署到服務器,我只有ftp訪問,應用程序的名稱是test.foo.com。有一個用戶上傳圖片到應用程序的後端。一切都很好。其代碼如下:Server.MapPath返回不正確的路徑

//in web config this is the value 
<add key="NewsImagesPath" value="~/App_Data/NewsImages/" /> 

// this is in controller 
private static readonly string news_images_path = ConfigurationManager.AppSettings["NewsImagesPath"]; 

// in the method 
String uploadedFile = fileUploadHelper.UploadFile(file, Server.MapPath(news_images_path)); 

這裏的fileuploadhelper返回上傳的路徑:

public class FileUploadHelper 
{ 
    public string UploadFile(HttpPostedFileBase file, string path) 
    { 
     if (file != null && file.ContentLength > 0) 
     { 
      FileInfo fileInfo = new FileInfo(file.FileName); 
      string fileName = Guid.NewGuid() + fileInfo.Extension; 
      var uploadPath = Path.Combine(path, fileName); 

      file.SaveAs(uploadPath); 
      return uploadPath; 
     } 

     return null; 
    } 
} 

嗯,這代碼工作正常。

問題是當這個應用程序被部署到foo.com。照片仍在上傳到test.foo.com App_Data文件夾。

即:我上傳的foo.com和圖像的圖像被存儲在:

c:\inetpub\wwwroot\test.foo.com\App_Data 

,而應該去

c:\inetpub\wwwroot\foo.com\App_Data 

這究竟是爲什麼?

我不知道如何配置服務器,IIS。

+0

您需要了解IIS的配置方式。主機名綁定不會影響應用程序根目錄在文件系統中的位置。 –

+0

我需要在IIS配置中查看哪些內容? – DarthVader

+0

應用程序主目錄是重要的。你不能通過FTP查看,所以問問你的主機。 –

回答

1

Server.MapPath("~")指向ASP.NET應用程序配置爲在其下運行的物理根文件夾。所以我猜想在IIS中有一些配置,因此test.foo.comfoo.com實際上都指向同一個應用程序。如果您無法訪問服務器進行檢查,除聯繫您的託管服務提供商之外,您可以做的事情不多,並要求提供有關如何設置這些域以及它們映射到的應用程序的更多詳細信息。

+0

謝謝。我會聯繫主機提供商。 – DarthVader

相關問題