2016-10-24 242 views
0

我在IIS 7.5中有一個網站,並嘗試將文件上載到位於另一個驅動器上的「文件」文件夾中。我添加了虛擬文件夾文件wwwroot文件夾。IIS中的IEnvironment.WebRootPath和虛擬目錄

該虛擬文件夾的路徑是d:\文件

我在控制器上載代碼:

_env變量是IHostingEnvironment和構造注射。

var filename = _env.WebRootPath + [email protected]"\files\{model.JobID}.{model.FileExt}"; 
using (FileStream fs = System.IO.File.Create(filename)) 
{ 
    AttachFile.CopyTo(fs); 
    fs.Flush(); 
} 

它在本地工作,因爲我的機器上有物理文件夾wwwroot \ files。不過,這並不在生產服務器上運行,並得到了以下錯誤:

An unhandled exception has occurred: Could not find a part of the path 'C:\inetpub\Mysite\wwwroot\files\111214.png'. 

但我已經拿到了另一個網站的ASPNET 4.6.1和我以前使用Server.Mappath上傳的文件。它可以在該網站上運行,我可以成功上傳文件。

string SaveLocation = string.Format(@"{0}\{1}{2}", Server.MapPath("files"), JobID, FileExt); 

enter image description here

根據這篇文章,它說和使用Server.Mappath是WebRootPath類似。 http://www.mikesdotnetting.com/article/302/server-mappath-equivalent-in-asp-net-core

但是,在我看來,WebRootPath只給了我們網站的根路徑。它不接受參數來評估給定的URL,如Server.MapPath。

您能否告訴我如何上傳生產服務器上的文件,而不是在我的.Net Core應用程序中硬編碼物理路徑?

更新1:

這是我迄今爲止達到最佳的...我們可以訪問或創建另一個虛擬URL指向這樣其他位置。它可以通過http://mysite/Files/abc.jpg

參考訪問:ASPNetCore StaticFiles

app.UseStaticFiles(new StaticFileOptions() 
    { 
     FileProvider = new PhysicalFileProvider(@"D:\Files"), 
     RequestPath = new PathString("/Files"),     
    }); 

然而,這僅僅是隻讀的網址,我不能用「/Files/newfile.jpg」或Path.Combine上傳到這個路徑(_env .WebRootPath,「newfile.jpg」),因爲它沒有物理存在。

+0

WebRootPath不知道關於IIS vdirs,你必須通過IWebHostBuilder.UseWebRoot(mywebroot) – Tratcher

+0

直接配置它你知道如何解決這個問題嗎? – Ruchan

+0

@Ruchan您可以直接使用「D:\ mySharedfiles \」代替IIS虛擬文件夾 – TTCG

回答

0

這裏是你如何配置內容和Web根:

public class Program 
{ 
    public static void Main(string[] args) 
    { 
     var contentRoot = Directory.GetCurrentDirectory(); 

     var config = new ConfigurationBuilder() 
      .SetBasePath(contentRoot) 
      .AddJsonFile("hosting.json", optional: true) 
      .Build(); 

     //WebHostBuilder is required to build the server. We are configurion all of the properties on it 
     var hostBuilder = new WebHostBuilder() 

      .UseKestrel() 

      //Content root - in this example it will be our current directory 
      .UseContentRoot(contentRoot) 

      //Web root - by the default it's wwwroot but here is the place where you can change it 
      //.UseWebRoot("wwwroot") 

      //Startup 
      .UseStartup<Startup>() 

      .UseConfiguration(config); 

     var host = hostBuilder.Build(); 
     //Let's start listening for requests 
     host.Run(); 
    } 
} 

可能UseWebRoot()和UseContentRoot()是你在找什麼。

+0

直接寫入物理文件夾。請您詳細說明它是如何幫助我解決問題的?據我所知,這是用來指向wwwroot文件夾(WebRoot)和Content Root文件夾。我如何使用WebRoot或ContentRoot指向D:\文件中的文件? 如果我使用ContentRoot指向那個路徑,我所有的視圖都沒有找到......它在D:\ files文件夾中找到視圖。 – TTCG

+0

看看這裏:https://docs.asp.net/en/latest/fundamentals/static-files.html。這是描述如何使用靜態文件的文章。在我看來,如果所有這些改進(可能性改變根 - UseWebRoot()和UseContentRoot())你仍然無法解決你的問題,你應該退後一步,想想如果你真的必須在「D :\文件」。也許將它們存儲在數據庫中 - SQL Server支持非常棒的「文件表」功能。也許試試這種方法。 –

+0

是的,我必須。這些文件與其他2個Web應用程序共享。我們可以通過在經典的.Net中使用Server.MapPath(xxx)輕鬆實現相同的功能。所以,我認爲有可能在.Net Core中做同樣的事情。顯然,答案是否:(我必須在appSettings.json中硬編碼路徑。謝謝你的回覆。 – TTCG