c# - 从 bin 以外的文件夹加载程序集

标签 c# .net asp.net-mvc asp.net-mvc-3

我创建了一个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());

    }

最佳答案

另请参阅

C#: Custom assembly directory

您可以向 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/

相关文章:

.net - Windows SDK 中的 NetFx 是什么?

c# - 有没有办法使 View 状态过期?

c# - 如何使用 Entity Framework 和 MVC4 在 Multi-Tenancy 应用程序中过滤 DbContext

c# - .NET CSV uploader 允许空值

c# - 动态扩展SQL语句-页面搜索

c# - 我的网络应用程序应该有多大的弹性?

jquery - asp.net mvc 2 可以将 View 呈现为 jquery-tmpl 模板吗?

c# - 将 double 格式化为小数点后 8 位

c# - 如何在 Visual Studio 中创建一个已经存在的数据库?

c# - 要再次显示网页,浏览器需要重新发送信息