代码之家  ›  专栏  ›  技术社区  ›  Dennis

如何在Razor页面应用程序中从插件动态加载页面?

  •  2
  • Dennis  · 技术社区  · 7 年前

    我正在尝试使用Razor页面应用程序处理插件。

    不能

    enter image description here

    没有什么特别的内页。功能页只生成简单的HTML。索引页生成一种菜单。

    索引页模型:

    public class IndexModel : PageModel
    {
        public IEnumerable<MenuItem> MenuItems { get; private set; }
    
        public void OnGet()
        {
            MenuItems = new List<MenuItem>
            {
                new MenuItem { Route = "FeatureA", Title = "Feature A" },
                new MenuItem { Route = "FeatureB", Title = "Feature B" }
            };
        }
    }
    

    索引页:

    @page
    @model IndexModel
    @{
        ViewData["Title"] = "Home page";
    }
    
    <div class="text-center">
        <div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
            <ul class="navbar-nav flex-grow-1">
                @foreach (var item in Model.MenuItems)
                {
                    <li class="nav-item">
                        <a class="nav-link text-dark" asp-area="" asp-page="/@item.Route">@item.Title</a>
                    </li>
                }
            </ul>
        </div>
    </div>
    

    当我运行应用程序时,有一些菜单项,但是 href s为空:

    <div class="text-center">
        <div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
            <ul class="navbar-nav flex-grow-1">
                    <li class="nav-item">
                        <a class="nav-link text-dark" href="">Feature A</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link text-dark" href="">Feature B</a>
                    </li>
            </ul>
        </div>
    </div>
    

    当然,所有程序集(应用程序和功能程序集)都在同一目录中。

    以下两种情况下菜单有效:

    • 或者我在appproject中引用RCL项目,这会扼杀插件的想法;
    • 或者如果我把 App.deps.json 具有 FeatureLib_A 和 FeatureLib_B 作为依赖项(只保存第一个案例中的deps文件,删除引用,全部重建,复制保存的deps文件)。

    同时,我也试着迫不及待地把RCL组件加载进去 Startup 班级。 正在加载程序集,但是 Index page的行为是一样的。

    有没有什么方法可以告诉ASP基础结构在不修改deps文件的情况下使用RCL程序集?我错过了什么?

    1 回复  |  直到 7 年前
        1
  •  5
  •   Dennis    7 年前

    我已经想好了。

    基本思想是给予 ApplicationPartManager 适当的应用部件。
    需要注意的是:

    • “代码”组件(例如。 FeatureLib_A.dll )必须添加为 AssemblyPart
    • “查看”组件(例如。 FeatureLib_A.Views.dll )必须添加为 CompiledRazorAssemblyPart .

    样本代码:

    public class Startup
    {
        // ...
    
        public void ConfigureServices(IServiceCollection services)
        {
            var assemblyLoader = new DotNetCoreAssemblyLoader(searchPattern: "FeatureLib*.dll");
    
            services.AddMvc()
                .ConfigureApplicationPartManager(_ =>
                {
                    foreach (var assembly in assemblyLoader.Assemblies)
                    {
                        if (assembly.FullName.Contains("Views"))
                        {
                            _.ApplicationParts.Add(new CompiledRazorAssemblyPart(assembly));
                        }
                        else
                        {
                            _.ApplicationParts.Add(new AssemblyPart(assembly));
                        }
                    }
                })
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }
    
        // ...
    }
    

    DotNetCoreAssemblyLoader 是一个自定义类,它使用给定的搜索模式查找程序集文件,并通过加载程序集 AssemblyLoadContext.Default.LoadFromAssemblyPath

        2
  •  2
  •   Khaled M.Ramadan    7 年前
    public class Startup
    {
    
        public Startup( IHostingEnvironment hostingEnvironment)
        {
    
            _hostingEnvironment = hostingEnvironment;
        }
        private readonly IHostingEnvironment _hostingEnvironment;
    
    
    
        public void ConfigureServices(IServiceCollection services)
        {
            ...
            services.AddMvc()
                          .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
                          .ConfigureApplicationPartManager(ConfigureApplicationParts); ;
        }
    
    
    
        private void ConfigureApplicationParts(ApplicationPartManager apm)
        {
            string rootPath = _hostingEnvironment.ContentRootPath;
            var pluginsPath = Path.Combine(rootPath, "Plugins");
    
            var assemblyFiles = Directory.GetFiles(pluginsPath, "Plugin*.dll", SearchOption.AllDirectories);
            foreach (var assemblyFile in assemblyFiles)
            {
                try
                {
                    var assembly = Assembly.LoadFrom(assemblyFile);
                    if (assemblyFile.EndsWith(".Views.dll"))
                        apm.ApplicationParts.Add(new 
                               CompiledRazorAssemblyPart(assembly));
                    else
                        apm.ApplicationParts.Add(new AssemblyPart(assembly));
                }
                catch (Exception e) { }
            }
        }
    }