ASP MVC 无法从导入的 MEF 模块中找到视图

ASP MVC Cannot find Views from imported MEF module

我正在尝试从已编译和导入的 MEF 模块中以 .dll 的形式访问我项目中自行创建的模块目录中的视图。

编写 MEF dll 后,我尝试导航到配置的模块 URL 以检查模块及其控件是否已正确导入,一切看起来都很好,因为控制器正在尝试获取正确的 ActionView 但问题是无法找到 .cshtml 文件是我的想法。

我还有一个 customViewEngine,它根据指定的文件夹处理模块视图的映射。

customViewEngine的代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Dependency_Injection_MEF_MVC.Components;

public class CustomViewEngine : RazorViewEngine
{
    private List<string> _plugins = new List<string>();

    public CustomViewEngine(List<string> pluginFolders)
    {
        //_plugins = pluginFolders;

        DirectoryInfo d = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Modules/temp/"));//Assuming Test is your Folder
        FileInfo[] Files = d.GetFiles("*.dll"); //Getting Text files
        foreach (FileInfo file in Files)
        {
            _plugins.Add(file.Name.Substring(0, file.Name.IndexOf('.')));
        }


        ViewLocationFormats = GetViewLocations();
        MasterLocationFormats = GetMasterLocations();
        PartialViewLocationFormats = GetViewLocations();
    }

    public string[] GetViewLocations()
    {
        var views = new List<string>();
        views.Add("~/Views/{0}");

        _plugins.ForEach(plugin =>
            views.Add("~/Modules/temp/" + plugin + "/Views/Home/{0}")
        );

        return views.ToArray();
    }

    public string[] GetMasterLocations()
    {
        var masterPages = new List<string>();

        masterPages.Add("~/Views/Shared/{0}.cshtml");

        _plugins.ForEach(plugin =>
            masterPages.Add("~/Modules/temp/" + plugin + "/Views/Shared/{0}.cshtml")
        );

        return masterPages.ToArray();
    }
}

我得到这个错误,分析它我可以看到它检查的最后两个地方是它应该驻留的地方但是视图引擎无法找到它,即使它在那里。

所以tl:dr

创建了单独的项目以将它们组合成主项目中的 MEF 部分,在组合它们之后我尝试通过导航到模块控制器的网页来测试它,但我遇到了上述错误。

这与 DLL 文件锁定有关,基本上我只需要创建模块 dll 的影子副本,它应该允许您访问所有内容。

这是我在网上找到的一段代码,运行完美。可能必须更改项目中的路径定义。

private static void genShadowCopy(List<string> pluginFolders){



    DirectoryInfo PluginFolder = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "/Modules/");


    DirectoryInfo ShadowCopyFolder = new DirectoryInfo(HostingEnvironment.MapPath("~/Modules/temp"));

    String shadowFullPath = ShadowCopyFolder.FullName;

    Directory.CreateDirectory(ShadowCopyFolder.FullName);
    //clear out old plugins in previous shadowcopyfolder)
    foreach (var f in ShadowCopyFolder.GetFiles("*.dll", SearchOption.AllDirectories))
    {
        f.Delete();
    }
    //shadow copy files
    foreach (var plug in PluginFolder.GetFiles("*.dll", SearchOption.AllDirectories))
    {
        var di = Directory.CreateDirectory(ShadowCopyFolder.FullName);
        // NOTE: You cannot rename the plugin DLL to a different name, it will fail because the assembly name is part if it's manifest
        // (a reference to how assemblies are loaded: http://msdn.microsoft.com/en-us/library/yx7xezcf )

        String dif = di.FullName + plug.Name;

        File.Copy(plug.FullName, Path.Combine(di.FullName, plug.Name), true);

    }