例如,访问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。