例如,訪問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。