繁体   English   中英

当前上下文中不存在Server.MapPath

[英]Server.MapPath does not exist in the Current Context

我有一个C#Model Class,我试图访问.cshtml页面,该页面应该是一个电子邮件格式模板。 我正在使用以下代码:

string body = string.Empty;
using (StreamReader reader = new StreamReader(Server.MapPath("~/EmailConfTemplate.cshtml")))
{
     body = reader.ReadToEnd();
}

但我收到以下错误消息:

名称Server在当前上下文中不存在

代码中是否有任何错误,或者在POCO类中无法访问Server类。 请帮忙。

要在.Net管道中执行它,您可以将其作为HttpContext的实例找到

System.Web.HttpContext.Current.Server.MapPath()

代替

Server.MapPath("~/EmailConfTemplate.cshtml")

尝试使用

string fullPath = new DirectoryInfo(string.Format("{0}\\EmailConfTemplate.cshtml", HttpContext.Current.Server.MapPath(@"\"))).ToString();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM