【发布时间】:2019-06-23 03:12:40
【问题描述】:
我正在尝试使用 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");
【问题讨论】:
标签: c# asp.net-mvc asp.net-core razor-pages razorengine