如何将一个 MVC 应用程序的部分视图重用到另一个 MVC 应用程序中

How to reuse the partial view of One MVC application into another MVC application

如何将一个 MVC 应用程序的部分视图或模板重用到另一个 MVC 应用程序中。 我在一个解决方案中创建了两个 MVC 应用程序。应用程序是 DemoVirtualPathProvider 和 SimpleMVCApp。我在名为 _samplepartialview.cshtml 的 SimpleMVCapp 中添加了一个额外的局部视图。现在我想在 DemoVirtualPathProvider 应用程序中使用这个局部视图。那么任何人都可以帮助我解决这个问题。提前致谢。

这是我试过的。我添加了 EmbeddedVirtualFile class

   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Reflection;
   using System.Text;
   using System.Web;
   using System.Web.Hosting;

   namespace DemoVirtualPathProvider.Models
   {
        public class EmbeddedVirtualFile : VirtualFile
        {
           private readonly string virtualPath;
           private readonly Assembly assembly;

           public EmbeddedVirtualFile(string virtualPath) : base(virtualPath)
           {
                this.assembly = this.GetType().Assembly;
                this.virtualPath = VirtualPathUtility.ToAppRelative(virtualPath);
            }


            public override System.IO.Stream Open()
            {

                var resourceName = this.GetType().Namespace + "." +   virtualPath.Replace("~/", "").Replace("/", ".");
                return assembly.GetManifestResourceStream(resourceName);
            }
         }
     }

这是 EmbeddedVirtualPathProvider class 代码

     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Reflection;
     using System.Web;
     using System.Web.Hosting;

     namespace DemoVirtualPathProvider.Models
     {
            public class EmbeddedVirtualPathProvider : VirtualPathProvider
            {
                 private readonly Assembly assembly =      typeof(EmbeddedVirtualPathProvider).Assembly;

                 private readonly string[] resourceNames;

                 public EmbeddedVirtualPathProvider()
                 {
                       this.resourceNames = assembly.GetManifestResourceNames();

                 }

                 private bool IsEmbeddedResourcePath(string virtualPath)
                 {
                       var checkpath = VirtualPathUtility.ToAppRelative(virtualPath);
                       var resourceName = this.GetType().Namespace + "." + checkpath.Replace("~/", "").Replace("/",".");
                       return this.resourceNames.Contains(resourceName);
                 }

                 public bool IsFileExists(string virtualPath)
                 {
                       return IsEmbeddedResourcePath(virtualPath) || base.FileExists(virtualPath);
                 }


                 public override VirtualFile GetFile(string virtualPath)
                 {
                       if (IsEmbeddedResourcePath(virtualPath))
                       return new EmbeddedVirtualFile(virtualPath);
                       return base.GetFile(virtualPath);
                 }


                 public override System.Web.Caching.CacheDependency  GetCacheDependency(string virtualPath, System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart)
                {
                      if (IsEmbeddedResourcePath(virtualPath))
                           return null;
                      return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
                }
           }
      }

这是我的 _layout.cshtml DemoVirtualPathProvider 应用程序页面

     <!DOCTYPE html>
     <html lang="en">
     <head>
            <meta charset="utf-8" />
            <title>@ViewBag.Title - My ASP.NET MVC Application</title>
            <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
            <meta name="viewport" content="width=device-width" />
            @Styles.Render("~/Content/css")
            @Scripts.Render("~/bundles/modernizr")
            </head>
            <body>
            <header>
                <div class="content-wrapper">
                <div class="float-left">
                <p class="site-title">@Html.ActionLink("your logo here", "Index", "Home")</p>
                </div>
                <div class="float-right">
                   <section id="login">
                    @Html.Partial("_LoginPartial")
                    @Html.Partial("_SimplePartialView")
                   </section>
                   <nav>
                      <ul id="menu">
                        <li>@Html.ActionLink("Home", "Index", "Home")</li>
                        <li>@Html.ActionLink("About", "About", "Home")</li>
                        <li>@Html.ActionLink("Contact", "Contact", "Home") </li>
                      </ul>
                    </nav>
                    </div>
        </div>
    </header>
    <div id="body">
        @RenderSection("featured", required: false)
        <section class="content-wrapper main-content clear-fix">
            @RenderBody()
        </section>
    </div>
    <footer>
        <div class="content-wrapper">
            <div class="float-left">
                <p>&copy; @DateTime.Now.Year - My ASP.NET MVC Application</p>
            </div>
        </div>
    </footer>

    @Scripts.Render("~/bundles/jquery")
    @RenderSection("scripts", required: false)
</body>
</html>

在项目之间共享视图的唯一可靠方法是使用 Razor Generator。这会将您的 cshtml 视图编译成 C# 代码,您可以通过 class 库或类似库在项目之间共享这些代码,就像任何其他 C# 代码一样。