asp.net - 应如何使用 CompiledRazorAssemblyPart 加载 Razor View ?

标签 asp.net asp.net-core

我有一个 Razor 类库,在 ASP.NET Core MVC 项目中用作引用。该应用程序运行良好。我删除了引用并使用 CompiledRazorAssemblyPart 将 dll 添加到应用程序部件。这是示例代码Loading Razor Class Libraries as plugins

当我使用 RCL 作为引用时有效的相同路线将不再有效。我应该使用任何其他设置来加载 View 吗?

最佳答案

我在使用 CompiledRazorAssemblyPart 时遇到了类似的问题,事实证明问题是我将 RCL 程序集(例如 MyPlugin.dll)添加为 CompiledRazorAssemblyPart,而您需要添加 .Views 程序集(例如 MyPlugin.Views.dll)。下面是我现在用来添加主 Web 应用程序项目未引用的插件程序集的代码示例。请注意,您可能仍然希望插件程序集作为 Controller 等的标准应用程序部分。

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));
            }
        }

关于asp.net - 应如何使用 CompiledRazorAssemblyPart 加载 Razor View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51003853/

相关文章:

c# - 在数据库表或内存中跟踪在线用户是个好主意吗?

asp.net - 什么是最好的基于 Web 的 HTML 编辑器,它允许在插入图像时将图像上传到服务器?

c# - asp .net 核心 wcf 端点

c# - 在.Net Core库中使用IHostingEnvironment

c# - 使用 Entity Framework 从 Excel 工作表中读取数据

c# - 如何在详细 View 中为命令字段添加删除确认提示?

c# - 覆盖 ASP.NET Core 中的 Controller 名称

c# - 创建的 CookieAuthentication cookie 的内容?

api - AWS Cognito AccessToken 与 IdToken

asp.net - 使用 .Net 验证器在无效时将类添加到文本框