如果要得到傳統的ASP.Net應用程序中的相對路徑或虛擬路徑對應的服務器物理路徑,只需要使用使用Server.MapPath()方法來取得Asp.Net根目錄的物理路徑。
但是在Asp.Net Core中不存在Server.MapPath()方法,Controller基類也沒有Server屬性。
在Asp.Net Core中取得物理路徑:
從ASP.NET Core 2.0開始,可以通過注入 IHostingEnvironment 服務對象來取得Web根目錄和內容根目錄的物理路徑,IHostingEnvironment保留了應用程序的基本信息,如下所示
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; namespace ConsoleApp1 { public class HomeController : Controller { private readonly IHostingEnvironment _hostingEnvironment; public HomeController(IHostingEnvironment hostingEnvironment) { _hostingEnvironment = hostingEnvironment; } public ActionResult Index() {
//Web根目錄 string webRootPath = _hostingEnvironment.WebRootPath;
//內容根目錄 string contentRootPath = _hostingEnvironment.ContentRootPath; return Content(webRootPath + "\n" + contentRootPath); } } }
這里要注意區分Web根目錄 和 內容根目錄的區別:
Web根目錄是指提供靜態內容的根目錄,即asp.net core應用程序根目錄下的wwwroot目錄
內容根目錄是指應用程序的根目錄,即asp.net core應用的應用程序根目錄
在ASP.NET Core 2.0之前 (就是ASP.NET Core 1.0),通過 IApplicationEnvironment.ApplicationBasePath 來獲取 Asp.Net Core應用程序的根目錄(物理路徑) 。但是現在3都出來了,並且之前版本不完善,很多api都沒有,也就不推薦使用了。
當然也有其他方式獲取路徑:
System.IO
命名空間System.IO
中存在Directory類,提供了獲取應用程序運行當前目錄的靜態方法
System.IO.Directory.GetCurrentDirectory()
=>Gets the current working directory of the application,
var path = System.IO.Directory.GetCurrentDirectory(); Console.WriteLine(path);
輸出 C:\Users\LIKUI\source\repos\ConsoleApp1\ConsoleApp1\bin\Debug\netcoreapp2.2
2. 反射方法: 獲取當前執行dll所在目錄
var doPath = Assembly.GetEntryAssembly().Location; Console.WriteLine(doPath);
3. 反射方法: 動態方式獲取當前可執行類文件所在目錄
dynamic type = (new Program()).GetType(); string currentDirectory = Path.GetDirectoryName(type.Assembly.Location);

string webRootPath = _hostingEnvironment.WebRootPath;//為null
,
wwwroot
目錄,且沒有啟用靜態文件服務需要開啟服務。
在
Startup.cs
的
Configure
中添加
app.UseStaticFiles();
,並且在應用根目錄中添加文件夾命名為
wwwroot
即可
啟動程序Startup類:
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseStaticFiles(); app.UseHttpsRedirection(); app.UseMvc(); }
5. 修改mvc/api中wwwroot靜態文件夾的路徑
首先在wwwroot文件下放上test.txt文件內容為測試文件。
運行后訪問http://localhost:44395/test.txt
顯示為測試文件。
說明默認靜態文件起作用,如果不想在默認的應用程序下放wwwroot或者靜態文件路徑已經指向了固定位置,則需要使用StaticFileOptions
修改默認靜態文件夾的路徑

比如這里我引用本地桌面的一個文件
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } //引用自定義路徑靜態文件 var path = @"C:\Users\LIKUI\Desktop\試點項目\"; var staticFile = new StaticFileOptions(); staticFile.FileProvider = new PhysicalFileProvider(path); app.UseStaticFiles(staticFile); app.UseHttpsRedirection(); app.UseMvc(); }
如圖:
public void ConfigureServices(IServiceCollection services)
{
services.AddDirectoryBrowser();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } //引用自定義文件路徑 var path = @"C:\Users\LIKUI\Desktop\試點項目\"; var staticFile = new StaticFileOptions(); staticFile.FileProvider = new PhysicalFileProvider(path); app.UseStaticFiles(staticFile); //顯示靜態文件路徑下的所有文件 var staticBrowser = new DirectoryBrowserOptions(); staticBrowser.FileProvider = new PhysicalFileProvider(path); app.UseDirectoryBrowser(staticBrowser); app.UseHttpsRedirection(); app.UseMvc(); }
如圖:
.log
,
.conf
等為文本文件格式

public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } //引用自定義文件路徑 var path = @"C:\Users\LIKUI\Desktop\試點項目\"; var staticFile = new StaticFileOptions(); staticFile.FileProvider = new PhysicalFileProvider(path); staticFile.ServeUnknownFileTypes = true; staticFile.DefaultContentType = "application/x-msdownload";//設置默認MIME,此處為下載 var fileExtensionContentTypeProvider = new FileExtensionContentTypeProvider(); fileExtensionContentTypeProvider.Mappings.Add(".log", "text/plain");//設置特定類型的MIME fileExtensionContentTypeProvider.Mappings.Add(".conf", "text/plain"); staticFile.ContentTypeProvider = fileExtensionContentTypeProvider; app.UseStaticFiles(staticFile); //顯示靜態文件路徑下的所有文件 var staticBrowser = new DirectoryBrowserOptions(); staticBrowser.FileProvider = new PhysicalFileProvider(path); app.UseDirectoryBrowser(staticBrowser); app.UseHttpsRedirection(); app.UseMvc(); }
這樣就可以打開了