在ASP.NET Core解决方案中的项目之间共享视图(.cshtml文件) [英] Share views (.cshtml files) between projects in asp.net core solution

查看:600
本文介绍了在ASP.NET Core解决方案中的项目之间共享视图(.cshtml文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,由于各种原因,该项目具有许多基本相同的控制器和视图.目前,它们已在项目之间复制,因此每个项目中都有多个副本. 我想知道是否有将它们放在一个单独的项目(例如,一个类库项目)中并修改视图定位器功能(IViewLocationExpander?)或其他某种方法来使其在其他项目中查看这些视图的方法.

我知道在完整"的asp.net中可能会有一些黑客攻击,并且它从来都不是很干净或优雅的,所以想知道asp.net内核是否有更好的方法?

谢谢

解决方案

您可以创建AspNetCore类库并使用 StackOverflow答案所示.这应该允许您根据需要编译和共享Controllers和Views.视图(.cshtml文件)将不再需要部署,因为它们将被编译并在此类库中可用.

然后添加对该类库项目的引用(如果在相同的解决方案中),或者将其创建为Nuget包并将其添加到其他项目中. (这可以通过私有Nuget服务器或Local Nuget存储库文件夹来完成.)

更新:ASP.NET Core 2.1具有新的

Thanks

You could create a AspNetCore Class Library and use AspNetCore Precompiled Views. Also as indicated in this StackOverflow answer. 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.

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. (This can be done by a private Nuget server or Local Nuget repository folder).

Update: ASP.NET Core 2.1 has a new Razor SDK which makes this simpler. It emits a Views assembly (AppName.Views.dll).

这篇关于在ASP.NET Core解决方案中的项目之间共享视图(.cshtml文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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