ASP.NET 核心和视图冲突解决
ASP.NET Core and View conflict resolution
我正在尝试为 ASP.NET 核心应用程序构建一个插件系统。我在应用程序中有一个名为 'Plugins'.
的文件夹
一般来说,插件看起来像一个包含两个文件的文件夹:Plugin.dll 和 Plugin.Views.dll
该应用程序扫描此文件夹并加载这些程序集
foreach (var ext in extensionManager.Extensions)
{
mvcBuilder.AddApplicationPart(ext.Assembly);
if (ext.ViewsAssembly != null)
{
mvcBuilder.AddApplicationPart(ext.ViewsAssembly);
}
}
如果这些插件有同名的 ViewComponents,我会得到一个错误。 Plugin1 中的模型可能会传递到 Plugin2 的 Razor 视图中。
The model item passed into the ViewDataDictionary is of type 'Plugin1.Models.MyModel',
but this ViewDataDictionary instance requires a model item of type 'Plugin2.Models.MyModel'
我该如何解决这个冲突?
The model item passed into the ViewDataDictionary is of type
'Plugin1.Models.MyModel', but this ViewDataDictionary instance
requires a model item of type 'Plugin2.Models.MyModel'
如果此 ViewDataDictionary 实例需要类型为 Plugin2.Models.MyModel
的模型项,您可以尝试使用全名 Plugin2.Models.MyModel
。这样它就不会将 Plugin1.Models.MyModel
传递给此 ViewDataDictionary。
我通过将此 Target
添加到我的插件的 .csproj 文件中解决了这个问题:
<Target Name="UpdateTargetPath" BeforeTargets="AssignRazorGenerateTargetPaths">
<ItemGroup>
<RazorGenerate Link="$(TargetName)\%(RazorGenerate.RelativeDir)%(RazorGenerate.FileName)%(RazorGenerate.Extension)" />
</ItemGroup>
</Target>
我以为基础。我还从 RazorGenerate
标记中删除了 Include
属性,因为它在生成的程序集中重复了 RazorCompiledAttribute
。
在主机应用方面,我扩展了视图位置格式:
services.Configure<RazorViewEngineOptions>(opts =>
{
var pluginName = ext.Assembly.GetName().Name;
opts.ViewLocationFormats.Add($"/{pluginName}/Views/Shared/{{0}}.cshtml");
opts.ViewLocationFormats.Add($"/{pluginName}/{{1}}/Views/{{0}}.cshtml");
});
在插件中,我必须指定视图的完全限定路径:
return View($"/{this.GetType().Assembly.GetName().Name}/Views/Path/To/MyView.cshtml", model);
我正在尝试为 ASP.NET 核心应用程序构建一个插件系统。我在应用程序中有一个名为 'Plugins'.
的文件夹一般来说,插件看起来像一个包含两个文件的文件夹:Plugin.dll 和 Plugin.Views.dll 该应用程序扫描此文件夹并加载这些程序集
foreach (var ext in extensionManager.Extensions)
{
mvcBuilder.AddApplicationPart(ext.Assembly);
if (ext.ViewsAssembly != null)
{
mvcBuilder.AddApplicationPart(ext.ViewsAssembly);
}
}
如果这些插件有同名的 ViewComponents,我会得到一个错误。 Plugin1 中的模型可能会传递到 Plugin2 的 Razor 视图中。
The model item passed into the ViewDataDictionary is of type 'Plugin1.Models.MyModel',
but this ViewDataDictionary instance requires a model item of type 'Plugin2.Models.MyModel'
我该如何解决这个冲突?
The model item passed into the ViewDataDictionary is of type 'Plugin1.Models.MyModel', but this ViewDataDictionary instance requires a model item of type 'Plugin2.Models.MyModel'
如果此 ViewDataDictionary 实例需要类型为 Plugin2.Models.MyModel
的模型项,您可以尝试使用全名 Plugin2.Models.MyModel
。这样它就不会将 Plugin1.Models.MyModel
传递给此 ViewDataDictionary。
我通过将此 Target
添加到我的插件的 .csproj 文件中解决了这个问题:
<Target Name="UpdateTargetPath" BeforeTargets="AssignRazorGenerateTargetPaths">
<ItemGroup>
<RazorGenerate Link="$(TargetName)\%(RazorGenerate.RelativeDir)%(RazorGenerate.FileName)%(RazorGenerate.Extension)" />
</ItemGroup>
</Target>
我以RazorGenerate
标记中删除了 Include
属性,因为它在生成的程序集中重复了 RazorCompiledAttribute
。
在主机应用方面,我扩展了视图位置格式:
services.Configure<RazorViewEngineOptions>(opts =>
{
var pluginName = ext.Assembly.GetName().Name;
opts.ViewLocationFormats.Add($"/{pluginName}/Views/Shared/{{0}}.cshtml");
opts.ViewLocationFormats.Add($"/{pluginName}/{{1}}/Views/{{0}}.cshtml");
});
在插件中,我必须指定视图的完全限定路径:
return View($"/{this.GetType().Assembly.GetName().Name}/Views/Path/To/MyView.cshtml", model);