[英]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.