ASP.NET Core访问wwwroot下的文件

例如,访问wwwroot下的files/file.pdf

    public class SendEmailController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        private IWebHostEnvironment _hostEnvironment;
        public SendEmailController(IWebHostEnvironment hostEnvironment)
        {
            _hostEnvironment = hostEnvironment;
        }

        [HttpPost]
        public IActionResult Index(EmailModel model)
        {
            // get the file path
            string fileName = Path.Combine(_hostEnvironment.WebRootPath, "files", "file.pdf");

            // code omitted
return View(); } }

IHostingEnvironment在未来的版本中将不再支持,所以建议使用IWebHostEnvironment。

posted @ 2020-06-12 10:29  Kyle0418  阅读(4003)  评论(0编辑  收藏  举报