How to distribute razor views to another application in .NET Core

本秂侑毒 提交于 2019-12-24 10:13:45

问题


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

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.

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.

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


回答1:


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>

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

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



来源:https://stackoverflow.com/questions/45586641/how-to-distribute-razor-views-to-another-application-in-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!