asp.net Core webapi 获取应用程序所在目录的三种方式

1. string basePath1 = AppContext.BaseDirectory;
例如:D:\后端项目\testCore\test.WebApi\bin\Debug\net6.0\
2.string basePath2 =Path.GetDirectoryName(typeof(Program).Assembly.Location);
例如:D:\后端项目\testCore\test.WebApi\bin\Debug\net6.0\
3.从ASP.NET Core RC2开始,可以通过依赖注入 IHostingEnvironment 服务对象来取得Web根目录和内容根目录的物理路径,如下所示:
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
 
namespace AspNetCorePathMapping
{
    public class HomeController : Controller
    {
        private readonly IHostingEnvironment _hostingEnvironment;
 
        public HomeController(IHostingEnvironment hostingEnvironment)
        {
            _hostingEnvironment = hostingEnvironment;
        }
 
        public ActionResult Index()
        {
            string webrootPath = _hostingEnvironment.WebrootPath;
            string contentRootPath = _hostingEnvironment.ContentRootPath;
            //webrootPath: D:\后端项目\testCore\test.WebApi\wwwroot

          // contentRootPath: D:\后端项目\testCore\test.WebApi

            return Content(webrootPath + "\n" + contentRootPath);
        }
    }
}

  

相关文章

数组的定义 Dim MyArray MyArray = Array(1‚5‚123‚12‚98...
\'参数: \'code:要检测的代码 \'leixing:html或者ubb \'n...
演示效果: 代码下载: 点击下载
环境:winxp sp2 ,mysql5.0.18,mysql odbc 3.51 driver 表采...
其实说起AJAX的初级应用是非常简单的,通俗的说就是客户端(j...
<% ’判断文件名是否合法 Function isFilename(aFilename...