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

I have an ASP.NET MVC project that has a subfolder named email. It contains htm files for my email templates. At certain points on the site, I have a controller that needs to download one of these templates and send it as email.

What I'm trying to do is use reflection to get the path to the current executing assembly, but it does not work as I would expect. The path I return is as follows:

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 \ Email \ ProductAccountConfirmation.htm '

I find this strange because the site is hosted in IIS through Visual Studio. I would think that this would give me the dll location in my project folder in dev and the deployment folder for IIS during production.

+6
source share
5 answers

What you are looking for is Server.MapPath () , it will provide you with the path to the executable application.

+9
source

So, let's say you have a file called template1.html in a subfolder of your letters. This code should provide you with the full path to this HTML file.

 HttpContext.Current.Server.MapPath("/") + "\\emails\\template1.html"; 
+4
source
 AppDomain.CurrentDomain.BaseDirectory 
+3
source

ASP.Net creates a copy of the assemblies, and this is a copy that is used at runtime. Your dll files in the project folder are just templates.

Why do you need this directory? There may be an alternative way to achieve your goals.

+1
source

You can get your application root path with:

 @HostingEnvironment.ApplicationPhysicalPath 
+1
source

Source: https://habr.com/ru/post/892146/


All Articles