Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# RazorGenerator预编译MVCEngine无法跨程序集定位局部视图或编辑器模板_C#_Asp.net Mvc 4_Razor_Razorgenerator - Fatal编程技术网

C# RazorGenerator预编译MVCEngine无法跨程序集定位局部视图或编辑器模板

C# RazorGenerator预编译MVCEngine无法跨程序集定位局部视图或编辑器模板,c#,asp.net-mvc-4,razor,razorgenerator,C#,Asp.net Mvc 4,Razor,Razorgenerator,我有一个大型MVC4站点,每个MVC区域分为一个项目。我们使用RazorGenerator将所有视图预编译到项目程序集中进行部署,并将预编译MVCEngine用于视图引擎 我刚刚创建了一个新区域,我想共享另一个程序集的共享的视图,但在尝试定位部分视图时,我会遇到一个无效操作异常,并且似乎也找不到任何DisplayTemplates或EditorTemplates 我认为,这与中描述的问题类似 RazorGenerator应用程序_Start中的代码如下: var assemblies=新列表()

我有一个大型MVC4站点,每个MVC区域分为一个项目。我们使用RazorGenerator将所有视图预编译到项目程序集中进行部署,并将
预编译MVCEngine
用于
视图引擎

我刚刚创建了一个新区域,我想共享另一个程序集的
共享的
视图,但在尝试定位部分视图时,我会遇到一个
无效操作异常
,并且似乎也找不到任何DisplayTemplates或EditorTemplates

我认为,这与中描述的问题类似

RazorGenerator应用程序_Start中的代码如下:

var assemblies=新列表()
{
Tuple.Create(“Areas/Area1”,typeof(ABC.Project1.AreaRegistration.Assembly),
Tuple.Create(“Areas/Area2”,typeof(ABC.Project2.AreaRegistration).Assembly),
};
//摆脱默认的视图引擎
ViewEngines.Engines.Clear();
foreach(程序集中的变量程序集)
{
var引擎=新的预编译MVCEngine(assembly.Item2,assembly.Item1){
UsePhysicalViewsIfNewer=HttpContext.Current.Request.IsLocal
};
//允许共享区域1与区域2共享视图
如果(assembly.Item1==“区域/区域2”)
{
var sharedPaths=new[]{“~/Areas/Area1/Views/Shared/{0}.cshtml};
engine.ViewLocationFormats=engine.ViewLocationFormats.Concat(SharedPath.ToArray();
engine.MasterLocationFormats=engine.MasterLocationFormats.Concat(SharedPath.ToArray();
engine.PartialViewLocationFormats=engine.PartialViewLocationFormats.Concat(SharedPath.ToArray();
}
ViewEngines.Engines.Insert(0,引擎);
VirtualPathFactoryManager.RegisterVirtualPathFactory(引擎);
}
当我在区域2中的视图中遇到部分引用时,比如
@Html.partial(“Partials/Footer”)
,我会得到异常。看起来Razor正在寻找正确的路径

System.InvalidOperationException: The partial view 'Partials/Footer' was not found or no view engine supports the searched locations. The following locations were searched:
    ~/Areas/Area2/Views/Home/Partials/Footer.cshtml
    ~/Areas/Area2/Views/Shared/Partials/Footer.cshtml
    ~/Views/Home/Partials/Footer.cshtml
    ~/Views/Shared/Partials/Footer.cshtml
    ~/Areas/Area1/Views/Shared/Partials/Footer.cshtml
at System.Web.Mvc.HtmlHelper.FindPartialView(ViewContext viewContext, String partialViewName, ViewEngineCollection viewEngineCollection)
查看
预编译MVCEngine
的源代码,它似乎只查找程序集中的视图

我最初认为视图系统在尝试解析路径时会查看所有注册的视图引擎,但这似乎是一个错误的假设(我可以理解为什么不这样做)

有没有办法跨多个部件共享视图

更新

我通过创建一个自定义版本的
预编译MVCEngine
来解决这个问题,该版本在其构造函数中获取一个程序集列表。核心变化是:

\u mappings=新字典(StringComparer.OrdinalingOreCase);
foreach(组件中的var kvp)
{
var baseVirtualPath=NormalizeBaseVirtualPath(kvp.Key);
var组件=kvp.值;
var mapping=来自程序集中的类型。GetTypes()
其中typeof(WebPageRenderingBase).IsAssignableFrom(type)
让pageVirtualPath=type.GetCustomAttributes(继承:false).OfType().FirstOrDefault()
其中pageVirtualPath!=null
选择新的KeyValuePair(组合虚拟路径(baseVirtualPath,pageVirtualPath.VirtualPath),类型);
foreach(映射中的var映射)
{
_添加(map);
}
}

这种方法有更好的选择或缺陷吗?

您是否能够以其他方式使其工作?此更改是否添加到核心开源项目中的预编译MVC引擎?您是否尝试过使用CompositePrecompiledMvcViewEngine?这与您在更新中所做的类似,因为它允许使用多个程序集。