如何将剃刀视图分发到.NET Core中的另一个应用程序 [英] How to distribute razor views to another application in .NET Core

查看:108
本文介绍了如何将剃刀视图分发到.NET Core中的另一个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了Web应用程序-.NET Core中的Asp.Net MVC.

I have created web application - Asp.Net MVC in .NET Core.

此应用程序包含一些Razor Views,但我想与其他应用程序共享这些视图,例如使用DLL或中间件.

This application contains some Razor Views but I would like to share these views to another application like for example with DLL or like middleware.

以下是有关分布Controllers的示例的一些信息,但在Views周围没有什么特别的地方-

Here is some information about example with distribution Controllers but around Views nothing special - https://docs.microsoft.com/en-us/aspnet/core/mvc/advanced/app-parts

我已经尝试过像这样添加控制器:

I've tried add Controller like this:

var assembly = typeof(Project.HomeController).GetTypeInfo().Assembly;

services.AddMvc()
        .AddApplicationPart(assembly);

这很好,但是我不知道如何添加视图.

This works very well, but I don't know how add the Views.

如何将Razor Views分发到另一个应用程序?是将它们像中间件一样导入到MVC middleware吗?

How can I distribute the Razor Views to another application? Is it way import them like a middleware to the MVC middleware?

推荐答案

您可以创建一个普通的netstandard1.6库,即您的控制器所在的库,然后使用以下命令将视图资源嵌入到csproj中的该dll中:/p>

You can create a normal netstandard1.6 library-i.e., where your controllers are, and embed the view resources into that dll in your csproj using the following:

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

在那之后,您可以使用RazorViewEngineOptions注册这些:

After that, you can then register these using the RazorViewEngineOptions:

// Add views provided in this assembly.     
services.Configure<RazorViewEngineOptions>(options =>
{
    options.FileProviders.Add(
        new EmbeddedFileProvider(typeof(ClassInLibrary).GetTypeInfo().Assembly));
});

"ClassInLibrary"是您的库中的一个类,您可以从中获取程序集信息.

Where "ClassInLibrary" is a class in your library that you can then get the assembly information from.

这篇关于如何将剃刀视图分发到.NET Core中的另一个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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