Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get path of ASP.NET MVC site in the file system

I've an ASP.NET MVC project that has a sub folder called emails. This contains HTM files for my email templates. At certain points in the site, I have a controller that needs to load one of these templates and send it as an email.

What I'm trying to do is use reflection to get the path of the current executing assembly but it's not working as I would expect it. The path I am getting back is:

var directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET iles\ssl\1da130c4\f8e7810e\assembly\dl3\5f253aca\1a71f123_e83bcc01\Emails\ProductAccountConfirmation.htm'

I find this strange as the site is being hosted in IIS via Visual Studio. I would have thought this would give me the dll location in my project folder in dev and the deploy folder for IIS in production.

like image 250
Daniel Revell Avatar asked Jul 06 '11 14:07

Daniel Revell


2 Answers

What you are looking for is Server.MapPath(), it will give you the path of the application that you are executing.

like image 84
Filip Ekberg Avatar answered Oct 21 '22 07:10

Filip Ekberg


So let's say you have a file called template1.html in your emails sub-folder. This code should give you the full path of that HTML file.

HttpContext.Current.Server.MapPath("/") + "\\emails\\template1.html";
like image 30
James Lawruk Avatar answered Oct 21 '22 06:10

James Lawruk