RaelB RaelB - 3 months ago 12
C# Question

How to load a file into a string in asp.net mvc

I am sending some email in an asp.net mvc application, and I want to place some html templates in a folder.

If I place a template file in a folder under Models e.g.

Models\EmailTemplates\MyTemplate.html
how do I load this file into a string (to be later used as the email body). I want to use
System.IO.File.ReadAllText
. How do I reference the file location?

Answer
string path = "~/Models/EmailTemplates/MyTemplate.html";
string html = System.IO.File.ReadAllText(System.Web.HttpContext.Current.Server.MapPath(path));

Use Server.MapPath