c# - 如何更改 .Views.dll 中预编译视图的默认命名并使用该名称通过控制器操作调用?
问题描述
我正在尝试使用 ASP.Net Core 2.2 和 MVC 制作模块化架构。本质上,我有一个基本的 ASP.Net Core 2.2 Web 应用程序。然后我想继续添加类库作为我的应用程序的功能。我通过配置 AreaViewLocationFormats 路由到这些功能。我的类库的目标是 SDK - Microsoft.NET.Sdk.Razor。在构建时,我确实看到了 .Views.dll 并且我的基础应用程序能够扫描这些并添加到应用程序工厂。
在应用程序启动时,我正在扫描这些类库的 dll 和 Views.dll,然后像这样将它们添加到应用程序部分(我已经简化了这个以便我可以共享):
foreach (var file in plugInFolder.GetFileSystemInfos("*.dll", SearchOption.AllDirectories))
{
Assembly assembly;
try
{
assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(file.FullName);
}
catch (FileLoadException ex)
{
// If assembly is already loaded, we'll just catch it and continue to the next one, plugins can have same dependencies:
if (ex.Message == "Assembly with same name is already loaded")
{
continue;
}
throw;
}
//If its a View Assembly then we need to load it differently:
if (assembly.FullName.Contains(".Views"))
{
mvcBuilder.ConfigureApplicationPartManager(mgr => {
foreach (var b in CompiledRazorAssemblyApplicationPartFactory.GetDefaultApplicationParts(assembly))
{
mvcBuilder.ConfigureApplicationPartManager(apm => apm.ApplicationParts.Add(b));
}
});
}
// If plugin hasn't been loaded already
else if (!plugins.ContainsKey(pluginFolder.Name) && // plug in isn't loaded already and
pluginFolder.Name == assembly.GetName().Name) // plug in Name matches Folder Name {Our convention}
{
plugins.Add(pluginFolder.Name, new PlugInInfo { Name = pluginFolder.Name, Assembly = assembly, Path = pluginFolder.PhysicalPath });
//plugin load:
mvcBuilder.AddApplicationPart(assembly);
}
}
问题在于 Views.dll,因为它们具有以下奇怪的属性(通过 ILSpy 检查):
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: RazorCompiledItem(typeof(Views_Home_Index), "mvc.1.0.view", "/Views/Home/Index.cshtml")]
[assembly: RazorView("/Views/Home/Index.cshtml", typeof(Views_Home_Index))]
我认为由于上述编译中的“/Views/Home/Index.cshtml”,我只能在使用时调用我的视图
return View("/Views/Home/Index.cshtml")
问题是 - 我不能使用与其他类库相同的 Views-Home-Index 命名。现在,我可以在不预编译视图和使用 cshtml 文件的情况下解决问题。但我想拥有预编译视图的优势。我已经参考了 Razor SDK 的 MS 文档,但未能获得适当的帮助,或者我可能错过了一些东西。我能做些什么来修复编译视图的这些名称吗?请指导。
我相信命名来自每个 MVC 约定的视图的位置,但我希望能够通过以下方式调用我的视图
return View("<FeatureName>/Views/Home/Index.cshtml");
但目前它仅适用于:
return View("Views/Home/Index.cshtml");
解决方案
我联系了 aspnet 核心团队,我得到了答案。如果您也被困在这里,请参考以下内容并支持我的问题:) https://github.com/aspnet/AspNetCore/issues/7112
推荐阅读
- c++ - Qt - 使用多个查询将问题更新到 Sqlite
- arrays - 如何使用 Google 表格中的 Query() 函数添加每个数据行的行索引?
- javascript - classList.toggle 在 console.log 之前执行
- linux - 如何在 linux CLI 中访问 yaml 文件中的变量?
- ruby-on-rails - 将数组传递到 Ruby Excel 文档生成器
- python - 无法通过 python 网络爬虫搜索(搜索时 URL 不会更改)
- terraform - 无法通过 terraform 中的 ARM 模板将 API 连接连接到逻辑应用程序
- oracle - 无法在 Oracle 数据库中使用 SMT 转换提取字段
- sql - 列数取决于变量
- android - 有没有办法在没有 MANAGE_EXTERNAL_STORAGE 的情况下在 Android 11 上打开具有特定于应用程序扩展名的文件