Virtual Path Provider with embedded resource Asp.Net MVC as class library

710 views Asked by At

I have a requirement of seperating whole mvc structure into class libraries. But after seperation Virtual path provider must be implemented for getting the views from dll as embedded resources. I have multiple dll from which i need to get the path. Getting confused how to use the provider code to achieve the same from multiple assemblies. following is the code for my virtual path provider.

 public class EmbeddedVirtualPathProvider : System.Web.Hosting.VirtualPathProvider
    {
        public EmbeddedVirtualPathProvider()
        {

        }

        private bool IsEmbeddedResourcePath(string virtualPath)
        {
            var checkPath = VirtualPathUtility.ToAppRelative(virtualPath);
            return checkPath.StartsWith("~/Succeed.Web/", StringComparison.InvariantCultureIgnoreCase);
        }

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

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

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

And my virtual file

public class EmbeddedVirtualFile : VirtualFile
    {
        private string m_path;

        public EmbeddedVirtualFile(string virtualPath)
            : base(virtualPath)
        {
            m_path = VirtualPathUtility.ToAppRelative(virtualPath);
        }

        public override System.IO.Stream Open()
        {
            var parts = m_path.Split('/');
            var assemblyName = parts[1];
            var resourceName = parts[2];

            assemblyName = Path.Combine(HttpRuntime.BinDirectory, assemblyName);
            var assembly = System.Reflection.Assembly.LoadFile(assemblyName + ".dll");

            if (assembly != null)
            {
                return assembly.GetManifestResourceStream(resourceName);
            }
            return null;
        }
    }

Please let me know where iam doing wrong. or any thing which i need to do to mak it working. Thanc in advance

0

There are 0 answers