从另一个项目/程序集访问asp.net核心中的预编译视图

LDJ*_*LDJ 5 c# asp.net-mvc razor .net-core asp.net-core

这个问题开始,我现在在asp.net核心应用程序中设置了预编译视图,该程序使用命令行从命令行编译为DLL。

dotnet剃须刀预编译

命令。然后我将其打包为nuget包,使用

网络包

并将该软件包添加为对我从中删除视图的项目的引用。然后,我创建了一个新类,该新类IViewLocationExpandersetup.cs我的项目的方法中实现并设置了该类,并且可以看到它在我的新位置搜索视图。但是,我不知道将什么作为预编译视图的搜索路径,因为其中没有.cshtml文件。我只是得到一个InvalidOperationException与找不到视图。

有没有人做过此事或能够建议我如何将这些预编译的视图添加到搜索路径?

谢谢

Chr*_*rdt 2

我很惊讶,它直接这样工作:

我刚刚在我的主项目中注册了一个自定义 ViewExpander

services.AddMvc().AddRazorOptions(options =>
{
    options.ViewLocationExpanders.Clear();
    options.ViewLocationExpanders.Add(new TestViewLocationExpander());
};
Run Code Online (Sandbox Code Playgroud)

扩展器本身:

public class TestViewLocationExpander : IViewLocationExpander
{
    public void PopulateValues(ViewLocationExpanderContext context)
    {
    }

    public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }
        if (viewLocations == null)
        {
            throw new ArgumentNullException(nameof(viewLocations));
        }

        yield return "~/Test/Test.cshtml";
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我引用了我的另一个项目的*.PrecompiledViews.dll,其中包含一个Test/Test.cshtml。

瞧,我的主应用程序中的每一页都显示了这个。