在ASP.NET Core MVC中找不到嵌入式视图 [英] Embedded view can not be found in ASP.NET Core MVC

查看:536
本文介绍了在ASP.NET Core MVC中找不到嵌入式视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能从以下位置重复使用我的cshtml文件另一个程序集.这是裸露的样本:

I can't reuse my cshtml files from another assembly. Here's the bare-bone sample:

  • 使用默认模板(使用Web应用程序,模型视图控制器)创建一个ASP.NET Core Web应用程序项目,并将其命名为ViewReuse
  • 添加一个名为ViewLibrary
  • 的类库
  • 在ViewLibrary中添加对Microsoft.AspNetCore.All元数据包的引用
  • 创建一个名为Views的文件夹,然后创建另一个名为Shared的文件夹,并在其中创建一个名为ReusedLayout.cshtml
  • 的简单cshtml文件.
  • EmbeddedResources Include='Views\**\*.cshtml'添加到ViewLibrary的csproj,以将ViewLibrary.dll中的所有视图包括在内
  • 在ViewReuse项目的Startup.cs中,将MVC服务的配置更改为services.AddMvc().ConfigureApplicationPartManager(p => { p.ApplicationParts.Add(new AssemblyPart(typeof(ReusedController).Assembly)); });
  • 更改About.cshtml以使用ViewLibrary中的布局:Layout = "/Views/Shared/ReusedLayout.cshtml"
  • 然后运行该应用程序,并导航到/home/about.
  • Create an ASP.NET Core Web Application project with default template (using Web-Application, Model-View-Controller), and name it ViewReuse
  • Add a class library called ViewLibrary
  • Add a reference to Microsoft.AspNetCore.All metapackage in ViewLibrary
  • Create a folder called Views, then create another folder called Shared, and inside it create a simple cshtml file called ReusedLayout.cshtml
  • Add EmbeddedResources Include='Views\**\*.cshtml' to csproj of ViewLibrary, to include all views inside the ViewLibrary.dll
  • In ViewReuse project, inside Startup.cs, change configuration of MVC service to services.AddMvc().ConfigureApplicationPartManager(p => { p.ApplicationParts.Add(new AssemblyPart(typeof(ReusedController).Assembly)); });
  • Change About.cshtml to use the layout from ViewLibrary: Layout = "/Views/Shared/ReusedLayout.cshtml"
  • Then run the application, and navigate to /home/about.

对我来说,我遇到了此错误:

For me I encountered this error:

InvalidOperationException:布局视图 找不到"/Views/Shared/ReusedLayout.cshtml".这 搜索了以下位置:/Views/Shared/ReusedLayout.cshtml

InvalidOperationException: The layout view '/Views/Shared/ReusedLayout.cshtml' could not be located. The following locations were searched: /Views/Shared/ReusedLayout.cshtml

我做错了什么?我该如何解决这个问题?

What have I done wrong? How do I solve this issue?

推荐答案

我通过以下方式解决了该问题:

I solved it by the following way:

首先,我使用Razor dll库项目创建了一个名为RazorDll的解决方案,并删除了其中的所有内容.现在为视图创建MVC结构.为了进行测试,我在此处添加了一个名为_Layout2的文件.

First I created a solution called RazorDll with a Razor dll library project and deleted everything in it. Now create the MVC structure for the views. For testing purpose, I added a file called _Layout2 there.

还要在项目根目录中添加一个空的Startup类,以便以后进行程序集检测:

Also add an empty Startup class in the project root for assembly detection later:

namespace RazorDll {
    public class Startup {
    }
}

将所有Razor档案都嵌入非常重要!您有两种方法可以做到这一点:

Its important to have all Razor filed embedded! You have two ways of doing this:

如果您有一个仅用于嵌入所有Razor视图的项目,则这是最佳选择,因为您不必手动为每个视图编辑文件属性.只需右键单击该项目,然后选择编辑项目文件,或使用任何文本编辑器打开相应的.csproj文件,并将以下内容插入<Project>

If you have a project just for embedding all Razor view, this is the best option since you don't have to edit file propertys for every view by hand. Just right click on the project and choose edit project file, or open the corresponding .csproj file with any text editor and insert the following in <Project>

  <ItemGroup>
    <EmbeddedResource Include="Views\**\*.cshtml">
      <CopyToOutputDirectory>Never</CopyToOutputDirectory>
    </EmbeddedResource>
  </ItemGroup>

手工嵌入单个Razor视图

只需右键单击Razor视图.cshtml文件,选择 properties 并将构建操作设置为 embedded ressource .您要从另一个.NET Core程序集中使用的每个视图都是必需的.

Embedd single Razor views by hand

Just right click on a Razor views .cshtml file, choose properties and set the build action to embedded ressource. Required for every view you want to use from another .NET Core assembly.

现在创建在这里称为MvcDemo的MVC项目.这是一个正常的ASP.NET Core 2.1 MVC项目(此处选择了LTS,但也应与2.2一起使用),该项目已链接到我们的Razor dll

Now create the MVC project, called MvcDemo here. This is a normal ASP.NET Core 2.1 MVC project (choosed the LTS here, but should also work with 2.2) that got linked to our Razor dll

要在装配体中搜索Razor视图,我们将其添加到Startup.ConfigureServices方法中:

To search for Razor views in the Assembly, we add it in Startup.ConfigureServices method:

var viewAssembly = typeof(RazorDll.Startup).GetTypeInfo().Assembly;
var fileProvider = new EmbeddedFileProvider(viewAssembly);
services.Configure<RazorViewEngineOptions>(options => {
    options.FileProviders.Add(fileProvider);
});

请注意typeof中的完全限定类型,以区分正在使用的MVC项目的Startup类(该类将不带名称空间加载)和Razor类库.

Notice the full qualified type in typeof to distinct between the Startup class of the consuming MVC project (which would be loaded without namespace) and the Razor class library.

您已准备好进行更改,例如_ViewStart.cshtml消耗我们的Razor类库中的测试_Layout2:

You're ready to change e.g. _ViewStart.cshtml to consume our test _Layout2 from our Razor class library:

@{
    Layout = "_Layout2";
}

一个简单的POC演示项目的结果:

Result of a simple POC demo project:

这篇关于在ASP.NET Core MVC中找不到嵌入式视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆