如下代码,wwwrootPath 则为wwwroot的路径。
- 注:其中旧的版本的"WebRootPath"属性是写在"IHostingEnvironment" 中,新版迁移到"IWebHostEnvironment"。
public class HomeController : Controller
{
private readonly IStudentRepository _studentRepository;
private readonly IWebHostEnvironment _webHostEnvironment;
public HomeController(IStudentRepository studentRepository,IWebHostEnvironment webHostEnvironment)
{
_studentRepository = studentRepository;
_webHostEnvironment = webHostEnvironment;
}
[HttpGet]
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(StudentCreateViewModel model)
{
var wwwrootPath=_webHostEnvironment.WebRootPath;
if (!ModelState.IsValid)
{
return View();
}
if (model.Photo!=null)
{
string uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "images");
}
//Student newStudent= _studentRepository.Add(student);
return RedirectToAction("Details",new { id= 10});
}
}
大家有兴趣的话,可以加QQ群:417151675 交流.NET技术。