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

泪湿孤枕 提交于 2019-12-22 04:24:47

问题


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.


回答1:


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




回答2:


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";



回答3:


AppDomain.CurrentDomain.BaseDirectory



回答4:


With ASP.Net, a copy is made of the assemblies, and it is the copy that is used at runtime. Your dlls in the project folder are merely templates.

For what purpose do you need this directory? There may be an alternative way of achieving your aims.




回答5:


You can get your application's root path with:

@HostingEnvironment.ApplicationPhysicalPath


来源:https://stackoverflow.com/questions/6598264/get-path-of-asp-net-mvc-site-in-the-file-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!