如果要得到傳統的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(); }
這樣就可以打開了