简体   繁体   中英

How to share Areas across projects in ASP.NET Core?

I need share some controllers and views in two or more projects because I need the same logic in the backend, but I don't want duplicate the code.

In Asp Net standard I can create nuget a package of an area (this is not so beautiful solution but it worked) https://retinalamps.wordpress.com/2013/10/12/creating-an-asp-net-mvc-nuget-package

In the Asp Net Core Web Application I can't create package. I would like to find a good practice solution.

When we had to solve this problem seven years ago, it was svn:externals feature that did it, and it works flawlessly to this day. I understand that similar feature in git is called submodules .

I would take advantage of organizing it as Feature slices in ASP MVC Core, this would save a lot of time down the road, I would recommend it in 2 steps.

  1. First, organize /put it an area called Features splices MSDN ref . wire it up as in the article and well explained.

    // 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. Second, embed Features in a Shared project or Portable Libraries . I have attached links on how to do this from R. Williams and MSDN.

In summary, since you already have it in a common area consider it to be a feature and embed it inside a portable lib or a Shared Project . A nice article by R. Williams.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM