【问题标题】:Accessing pre-compiled views in asp.net core from another project/assembly从另一个项目/程序集访问 asp.net 核心中的预编译视图
【发布时间】:2017-06-24 19:36:10
【问题描述】:

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

dotnet razor 预编译

命令。然后我使用

将其打包为 nuget 包

dotnet 包

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

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

谢谢

【问题讨论】:

  • 您找到解决方案了吗?
  • 赏金信息:我使用的是 net core 2.0。
  • This 可能会对您有所帮助。我在 2011 年或 2012 年就这样做了,但手头没有答案。

标签: c# asp.net-mvc razor asp.net-core .net-core


【解决方案1】:

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

我刚刚在我的主项目中注册了custom ViewExpander

services.AddMvc().AddRazorOptions(options =>
{
    options.ViewLocationExpanders.Clear();
    options.ViewLocationExpanders.Add(new TestViewLocationExpander());
};

扩展器本身:

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

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

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

【讨论】:

  • 我需要在另一个项目中使用一个项目的 *.PrecompiledViews.dll 中的 _Layout.cshtml。可行吗?
猜你喜欢
  • 2018-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-11
  • 2020-12-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多