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.

8

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()
4

Instead of

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

Try using

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest