开发者

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 ass开发者_开发技巧embly 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.


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


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


AppDomain.CurrentDomain.BaseDirectory


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.


You can get your application's root path with:

@HostingEnvironment.ApplicationPhysicalPath
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜