DarcyThomas DarcyThomas - 2 months ago 16
ASP.NET (C#) Question

Finding path to DLL from ASP.Net project

I have a MVC website from which I want to load a DLL from another project in the solution. This would be easy in a console app. The catch is I want to load the dll in an app domain (so can't use a project or file reference). The website project and the DLL project don't trust each other you see.

I have used build events to copy the DLL into the bin/Debug or bin/Release folder however when I run the app Calls to Assembly.GetExecutingAssembly.Location gives some temp folder with only the website.DLL the other dll in another folder, with no obvious relationship between the folders.

How do I find the path to a dll, to load it in an app domain, with no direct references, in a asp.net MVC site?

Answer

You can use

System.Web.HttpContext.Current.Server.MapPath(path);

or use HostingEnvironment.MapPath

System.Web.Hosting.HostingEnvironment.MapPath(path);

or (if you use ASP.NET core)

// DI
IHostingEnvironment _appEnvironment;
Path.Combine(_appEnvironment.ApplicationBasePath, "bin", "Debug", "YourDllName.dll");

where path is something like var path = "~/bin/Debug/YourDllName.dll";