Sharing Razor views across projects

前端 未结 3 1552
渐次进展
渐次进展 2020-12-15 17:37

I want to share the layout (Header, Navigation and Footer Razor views) across multiple ASP.NET MVC projects. How can I do that?

Can I create a custom NuGet package t

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-15 18:30

    There are new options (IMHO) better available. I would take advantage of organizing it as Feature splices 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 ExpandViewLocations(ViewLocationExpanderContext context,
          IEnumerable 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.

提交回复
热议问题