问题
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