配置文件
一、ASP.Net
1 2 3 4 5 6 7 8 |
public class HomeController : Controller { public ActionResult Index() { var _path = Server.MapPath("~/"); return Content(_path); } } |
C#
二、ASP.Net Core
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; namespace MyWebsite.Controllers { public class HomeController : Controller { private readonly IHostingEnvironment _hostingEnvironment; public HomeController(IHostingEnvironment hostingEnvironment) { _hostingEnvironment = hostingEnvironment; } public ActionResult Index() { return Content($"WebRootPath = {_hostingEnvironment.WebRootPath}\n" + $"ContentRootPath = {_hostingEnvironment.ContentRootPath}"); } } } |
2.1 cshtml View 中使用
1 2 3 4 5 |
@using Microsoft.AspNetCore.Hosting @inject IHostingEnvironment hostingEnvironment WebRootPath = @hostingEnvironment.WebRootPath <br /> ContentRootPath = @hostingEnvironment.ContentRootPath |
声明:本站内容来源于原创和互联网,尊重作者版权,转载请注明来源网址,欢迎收藏,谢谢!
发表评论