如何从助手内部获取asp.net核心中的server.MapPath [英] how to get server.MapPath in asp.net core from inside a helper

查看:59
本文介绍了如何从助手内部获取asp.net核心中的server.MapPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有这样的帮助方法:

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

我需要在asp.net核心中获得与Server.MapPath相同的结果

I need to get the equivalent of Server.MapPath in asp.net core

推荐答案

您需要注入IHostingEnvironment.然后:

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

在html助手中,您可以执行以下操作:

in an html helper you can do this:

((IHostingEnvironment)html.ViewContext.HttpContext.RequestServices.GetService(typeof(IHostingEnvironment))).ContentRootPath;

这篇关于如何从助手内部获取asp.net核心中的server.MapPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆