如何在ASP.NET Core中跨项目共享区域?

时间:2017-12-01 18:07:19

标签: c# asp.net asp.net-mvc asp.net-core

我需要在两个或多个项目中共享一些控制器和视图,因为我需要在后端使用相同的逻辑,但我不想复制代码。

在Asp Net标准中,我可以创建一个区域的包(这不是那么漂亮的解决方案,但它有效) https://retinalamps.wordpress.com/2013/10/12/creating-an-asp-net-mvc-nuget-package

在Asp Net Core Web应用程序中,我无法创建包。我想找到一个好的练习解决方案。

2 个答案:

答案 0 :(得分:1)

七年前,当我们不得不解决这个问题时,svn:externals功能就是这样做了,直到今天仍然完美无瑕。我了解git中的类似功能称为submodules

答案 1 :(得分:1)

我会利用在ASP MVC Core中将其组织为Feature splices,这将节省大量时间,我会分两步推荐它。

  1. 首先,整理 /将其置于名为Features拼接MSDN ref的区域。 将其连接起来,如文章所述,并做了很好的解释。

    // you're telling ASP that you've other Feature areas that it should look public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations) { // Error checking removed for brevity var controllerActionDescriptor = context.ActionContext.ActionDescriptor as ControllerActionDescriptor; string featureName = controllerActionDescriptor.Properties["feature"] as string; foreach (var location in viewLocations) { yield return location.Replace("{3}", featureName); } }

  2. 其次, 嵌入 功能 Shared projectPortable Libraries。我附上了链接 如何从R. Williams和MSDN做到这一点。

  3. 总之,由于您已将它放在公共区域,因此将其视为一项功能并将其嵌入portable lib or a Shared Project内。 R. Williams的一篇不错的文章。

    enter image description here

相关问题