Lara
Lara

Reputation: 3021

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 = 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.

Upvotes: 8

Views: 33396

Answers (2)

shayah
shayah

Reputation: 39

Instead of

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

Try using

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

Upvotes: 0

Tushar Gupta
Tushar Gupta

Reputation: 15913

To execute it inside a .Net pipeline, you can find it as an instance present in HttpContext

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

Upvotes: 21

Related Questions