简体   繁体   English

在asp.net核心解决方案中的项目之间共享视图(.cshtml文件)

[英]Share views (.cshtml files) between projects in asp.net core solution

I have a project which for various reasons have a number of controllers and views that are essentially the same. 我有一个项目,由于各种原因,有许多控制器和视图基本相同。 At the moment they are replicated between projects so there are several copies of each in each project. 目前,它们在项目之间进行复制,因此每个项目中都有几个副本。 I was wondering if there was anyway to put these in a separate project (eg a class library project) and amend the View Locator capabilities (IViewLocationExpander?) or some other means to get it to look in a different project for these views. 我想知道是否还有将它们放在一个单独的项目(例如类库项目)中并修改View Locator功能(IViewLocationExpander?)或其他一些方法让它在这些视图的不同项目中查找。

I know it was possible in “full” asp.net with a bit of hacking about and it was never very clean or elegant, so wondered if there was any better way with asp.net core? 我知道这可能在“完整”的asp.net中有一些黑客攻击而且从来都不是很干净或优雅,所以想知道asp.net核心是否有更好的方法?

Thanks 谢谢

You could create a AspNetCore Class Library and use AspNetCore Precompiled Views . 您可以创建AspNetCore类库并使用AspNetCore预编译视图 Also as indicated in this StackOverflow answer . 也如此StackOverflow答案中所示 That should allow you to compile and share the Controllers and Views as you want. 这应该允许您根据需要编译和共享控制器和视图。 The views (.cshtml files) would no longer need to be deployed as they would be compiled and available in this class library. 将不再需要部署视图(.cshtml文件),因为它们将在此类库中编译和可用。

Then either add a reference to this class library project (if in the same solution) or create it as a Nuget package and add it to the other projects. 然后添加对此类库项目的引用(如果在同一解决方案中)或将其创建为Nuget包并将其添加到其他项目。 (This can be done by a private Nuget server or Local Nuget repository folder). (这可以通过私有Nuget服务器或Local Nuget存储库文件夹完成)。

Update: ASP.NET Core 2.1 has a new Razor SDK which makes this simpler. 更新: ASP.NET Core 2.1有一个新的Razor SDK ,使这更简单。 It emits a Views assembly (AppName.Views.dll). 它会发出一个视图程序集(AppName.Views.dll)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 同一解决方案中两个ASP.NET Core项目之间的通信 - Communication between two ASP.NET Core projects in the same solution Asp.net 核心 3.0 在发布或构建时发布视图文件 (.cshtml) - Asp.net core 3.0 Publish Views files (.cshtml) on Publish or on Build 在MVC ASP.NET中的解决方案中的两个不同应用程序项目的不同视图之间导航 - Navigating between different Views from two different Application Projects within a Solution in MVC ASP.NET Asp.Net Core 2.2 将大型 Cshtml 文件转换为 Pdf - Asp.Net Core 2.2 Converting large Cshtml files into Pdf ASP.NET Core 2.2 如何在项目之间共享 web.config/launchSettings.json 文件 - ASP.NET Core 2.2 How to share web.config/launchSettings.json files among projects 如何在 ASP.NET Core 中跨项目共享区域? - How to share Areas across projects in ASP.NET Core? 项目之间的ASP.NET MVC 5共享包 - ASP.NET MVC 5 share bundles between projects asp.net和asp.net核心项目是否有可能在同一解决方案中“发挥出色”? - Is it possible for asp.net and asp.net core projects to “play nice” in the same solution? 两个 ASP.NET 核心项目之间的依赖注入 - Dependency injection between two ASP.NET Core projects 读取解决方案数据文件 ASP.Net Core - Read solution data files ASP.Net Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM