2013-04-07 54 views
0

我正在開發一個使用VS 2012的Asp.Net MVC 4應用程序。本地使用IIS Express作爲Web服務器。我在嘗試訪問作爲我的解決方案一部分的文件時遇到問題。我有以下操作方法:VS 2012&IIS Express。找不到路徑的一部分'C: Program Files(x86) IIS Express 〜 MyPics My.jpg'

public FileContentResult GetImage() 
     { 
      byte[] imageByte = System.IO.File.ReadAllBytes(@"/MyPics/My.jpg"); 
      string contentType = "image/jpeg"; 

      return File(imageByte, contentType); 
     } 

在第一行,我收到以下錯誤:

Could not find a part of the path 'C:\Program Files (x86)\IIS Express\~\MyPics\My.jpg' 

我知道,上面的路徑是不正確的,但我無法理解什麼路徑我應該提供以解決這個問題。

問候 爬完米什拉

回答

9

您可以使用使用Server.Mappath()來獲得實際的目錄是這樣的:)

byte[] imageByte = System.IO.File.ReadAllBytes(Server.MapPath("~/MyPics/My.jpg")); 

有人主張使用HostingEnvironment.MapPath(而不是:What is the difference between Server.MapPath and HostingEnvironment.MapPath?

+0

非常感謝。有用。 – 2013-04-07 15:54:15

+0

太棒了,到處尋找這個。日Thnx! – texian 2013-09-10 20:35:42