应该如何使用CompiledRazorAssemblyPart加载Razor视图? [英] How CompiledRazorAssemblyPart should be used to load Razor Views?
问题描述
我有一个Razor类库,在ASP.NET Core MVC项目中用作参考.该应用程序运行正常.我删除了引用,并使用 CompiledRazorAssemblyPart
将dll添加到应用程序部件中.这是示例代码将Razor类库作为插件加载
I have a Razor Class Library which is used as a reference in the ASP.NET Core MVC project. The app is working fine. I removed the reference and used CompiledRazorAssemblyPart
to add the dll to the application parts. Here is a sample code Loading Razor Class Libraries as plugins
当我使用RCL作为参考时,相同的路由不再起作用.我应该使用其他设置来加载视图吗?
The same route that worked when I used the RCL as a reference wont work anymore. Should I use any other settings to load the view?
推荐答案
我在使用 CompiledRazorAssemblyPart
时遇到了类似的问题,结果发现问题是我正在添加我的RCL程序集(例如MyPlugin.dll)作为 CompiledRazorAssemblyPart
,而您需要添加.Views程序集(例如MyPlugin.Views.dll).以下是我现在用来添加主Web应用程序项目未引用的插件程序集的代码示例.请注意,您可能仍希望将插件程序集作为控制器等的标准应用程序部分.
I had a similar issue with using CompiledRazorAssemblyPart
, and it turns out the problem was that I was adding my RCL assembly (e.g. MyPlugin.dll) as a CompiledRazorAssemblyPart
, whereas you need to add the .Views assembly (e.g. MyPlugin.Views.dll). Below is an example of the code I'm now using to add a plugin assembly that is not referenced by the main web application project. Note that you probably still want the plugin assembly as a standard application part for controllers etc.
public static void AddPluginApplicationPart(IMvcBuilder mvcBuilder, Assembly assembly)
{
mvcBuilder.AddApplicationPart(assembly);
Assembly viewsAssembly = null;
try
{
viewsAssembly = Assembly.Load(assembly.GetName().Name + ".Views");
}
catch (FileNotFoundException)
{
}
if (viewsAssembly != null)
{
// Adding this application part allows us to use compiled razor views from this plugin
var viewsApplicationPart = new CompiledRazorAssemblyPart(viewsAssembly);
mvcBuilder.ConfigureApplicationPartManager(manager => manager.ApplicationParts.Add(viewsApplicationPart));
}
}
这篇关于应该如何使用CompiledRazorAssemblyPart加载Razor视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!