我正在尝试从已编译和导入的MEF模块中访问View,作为项目中自创模块目录中的.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();
}
}
我收到此错误,分析它我可以看到它检查的最后两个位置应该驻留在哪里但是View引擎无法找到它,即使它在那里。
所以tl:dr
创建了sperate项目,将它们组合成一个主项目中的MEF部分,在编写它们之后,我尝试通过导航到Module的控制器的网页来测试它,在那里我遇到了上述错误。
答案 0 :(得分:0)
这与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);
}