如果要得到传统的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 + " " + 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:UsersLIKUIsource eposConsoleApp1ConsoleApp1inDebug etcoreapp2.2
2. 反射方法: 获取当前执行dll所在目录
var doPath = Assembly.GetEntryAssembly().Location; Console.WriteLine(doPath);
3. 反射方法: 动态方式获取当前可执行类文件所在目录
dynamic type = (new Program()).GetType(); string currentDirectory = Path.GetDirectoryName(type.Assembly.Location);
![](https://img2018.cnblogs.com/blog/1067173/201912/1067173-20191204152155226-236338496.png)
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
修改默认静态文件夹的路径
![](https://img2018.cnblogs.com/blog/1067173/201912/1067173-20191204160105586-511116539.png)
比如这里我引用本地桌面的一个文件
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:UsersLIKUIDesktop试点项目"; 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:UsersLIKUIDesktop试点项目"; 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
等为文本文件格式![](https://img2018.cnblogs.com/blog/1067173/201912/1067173-20191204172507598-580221059.png)
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:UsersLIKUIDesktop试点项目"; 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(); }
这样就可以打开了