Server. Mappath Does Not Exist in the Current Context
I Have a C# Model Class Where I Am Trying to Access A. Cshtml Page Which Is Supposed to Be an Email Format Template. I'm Using the Following Code: String Body...
I have a C# Model Class where I am trying to access a .cshtml page which is supposed to be an email format template. I'm using the following code:
string body = string.Empty;
using (StreamReader reader = new StreamReader(Server.MapPath("~/EmailConfTemplate.cshtml")))
{
body = reader.ReadToEnd();
}
But i am getting the following error message:
The name Server does not exist in the current context
Is there any error in the code or the Server class can't be accessed in POCO class. Please help.
2 Answers
To execute it inside a .Net pipeline, you can find it as an instance present in HttpContext
System.Web.HttpContext.Current.Server.MapPath()
Instead of
Server.MapPath("~/EmailConfTemplate.cshtml")
Try using
string fullPath = new DirectoryInfo(string.Format("{0}\\EmailConfTemplate.cshtml", HttpContext.Current.Server.MapPath(@"\"))).ToString();