我创建了一个mvc 3项目,命名空间是[POC.MVC.PluginHost]。 Controller 命名空间是[POC.MVC.PluginHost.Controllers]。我创建了一个类库项目并将其 namespace 更改为 [POC.MVC.PluginHost.Controllers]。
类库项目代码:
namespace POC.MVC.PluginHost.Controllers
{
public class BasicExampleController : Controller
{
public ActionResult Index()
{
// Add action logic here
throw new NotImplementedException();
}
public ActionResult Display()
{
return Content("");
}
}
}
我编译它并复制到mvc项目的bin目录,当我浏览http://localhost:xxxx/BasicExample/display
时它工作正常,但我想复制这个已编译的类的dll库位于其他文件夹(如 [plugin])中,但它不起作用,只有当我将其复制到我的 mvc 项目的 bin 文件夹时它才起作用。有办法解决这个问题吗?
编辑......................................
我在我的 web.config 中进行测试,如下所示:
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="Plugin" />
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
但这不行!!!
我测试了这个但这仍然不起作用......
static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
string assembliesDir = "Plugin";
string aa = System.IO.Path.Combine(assembliesDir, args.Name + ".dll");
aa = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, aa);
Assembly asm = Assembly.LoadFrom(aa);
return asm;
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
AppDomain.CurrentDomain.Load("SamplePlg");
System.Web.Hosting.HostingEnvironment.RegisterVirtualPathProvider(new AssemblyResourceProvider());
}
最佳答案
另请参阅
您可以向 app.config 添加其他搜索路径,以供其查找以加载程序集。例如
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="lib" />
</assemblyBinding>
</runtime>
关于c# - 从 bin 以外的文件夹加载程序集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12692216/