由於File.ReadAllText州約UnauthorizedAccessException,它可以由以下條件之一引起:
- 或 -
- 或 -
- 或 -
你可以利用捻控制檯和使用Attrib命令檢查屬性爲您的文件或目錄。此外,您可以嘗試使用TYPE命令顯示文件的內容或從文件目錄表單擊編輯按鈕,如下所示:

而且,我創建了一個新的Web應用程序,並部署了我MVC應用程序顯示App_Data
文件夾下的文件,它可以按預期工作,您可以參考it。
UPDATE:
//method for getting files
public List<DownLoadFileInformation> GetFiles()
{
List<DownLoadFileInformation> lstFiles = new List<DownLoadFileInformation>();
DirectoryInfo dirInfo = new DirectoryInfo(HostingEnvironment.MapPath("~/App_Data"));
int i = 0;
foreach (var item in dirInfo.GetFiles())
{
lstFiles.Add(new DownLoadFileInformation()
{
FileId = i + 1,
FileName = item.Name,
FilePath = dirInfo.FullName + @"\" + item.Name
});
i = i + 1;
}
return lstFiles;
}
//action for downloading a file
public ActionResult Download(string FileID)
{
int CurrentFileID = Convert.ToInt32(FileID);
var filesCol = obj.GetFiles();
string fullFilePath = (from fls in filesCol
where fls.FileId == CurrentFileID
select fls.FilePath).First();
string contentType = MimeMapping.GetMimeMapping(fullFilePath);
return File(fullFilePath, contentType, new FileInfo(fullFilePath).Name);
}
UPDATE2:
public ActionResult ViewOnline(string FileID)
{
int CurrentFileID = Convert.ToInt32(FileID);
var filesCol = obj.GetFiles();
string fullFilePath = (from fls in filesCol
where fls.FileId == CurrentFileID
select fls.FilePath).First();
string text = System.IO.File.ReadAllText(fullFilePath);
return Content(text);
}
什麼是在MapPath()路徑最終會被?您可以完全訪問'd:\ home \ site \ wwwroot'。 (注意:只要你不刪除你的網絡應用程序,'site'下的存儲是持久的,就像Azure存儲一樣)。 –
地圖路徑給了我正確的位置(至少在Kudu看),但它說無效訪問 –
您能否請編輯您的問題與更多的細節,包括完整的錯誤,以及哪些調用產生的錯誤? (我假設'ReadAllText()',但不知道肯定)。 –