如何从帮助器内部获取asp.net核心中的server.MapPath

Omu*_*Omu 4 asp.net-core

有一个这样的帮助方法:

 public static IHtmlContent Source(this IHtmlHelper html, string s)
 {
     var path = ServerMapPath() + "Views\\" + s;
Run Code Online (Sandbox Code Playgroud)

我需要在asp.net核心中获得相当于Server.MapPath的功能

reg*_*uld 8

你需要注射IHostingEnvironment.然后:

var path = env.ContentRootPath + "Views\\" + s;

在一个html帮助器中,你可以这样做:

((IHostingEnvironment)html.ViewContext.HttpContext.RequestServices.GetService(typeof(IHostingEnvironment))).ContentRootPath;
Run Code Online (Sandbox Code Playgroud)