这次是建立了asp.net mvc core项目,在controller中想获取网站在硬盘中的路径,找了一圈Server.MapPath() 已不存在,HttpContent也一样,经过查阅资料发现是如下方法来获取路径
将Controller增加构造方法,传入的参数为IHostingEnvironment对象实例。
而在Controller中的方法中可以通过IHostingEnvironment对象的WebRootPath属性获取到路径
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting; namespace WebApplication1.Controllers
{
public class UserController : Controller
{
private IHostingEnvironment host = null; public UserController(IHostingEnvironment host)
{
this.host = host;
} public IActionResult Login()
{
//host.WebRootPath
return View();
}
}
}