Search code examples
asp.net-mvc-4asp.net-mvc-partialviewvirtualpathprovider

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


How to reuse the partial view or templates of one MVC application into another MVC application. I have created two MVC applications in a single solution. The applications are DemoVirtualPathProvider and SimpleMVCApp. I have added one extra partial view in the SimpleMVCapp called _samplepartialview.cshtml. Now i want to use this partial view inside DemoVirtualPathProvider Application. So can any one help me to solve this. Thanks in advance.

This what i have tried. I have added 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);
            }
         }
     }

This is the EmbeddedVirtualPathProvider class code

     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);
                }
           }
      }

This my _layout.cshtml page of DemoVirtualPathProvider Application

     <!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>

Solution

  • The only reliable way to share views between projects is to use Razor Generator. This will compile your cshtml views into C# code that you can share between projects like any other bit of C# code through a class library or similar.