4

I would like to ask a question about loading static files (for example .png file) and using Virtual Path Provider.

So, I created project with App_Start:

    public static void Start()
    {
        ConfigureBundles();
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        //etc.
    }

    private static void ConfigureBundles()
    {
        BundleTable.EnableOptimizations = true;
        BundleTable.VirtualPathProvider = new EmbeddedVirtualPathProvider(HostingEnvironment.VirtualPathProvider);
    }

And prepare the overwrite method in another class:

// virtual path
public class EmbeddedVirtualPathProvider : VirtualPathProvider
{
    private VirtualPathProvider _previous;
    public EmbeddedVirtualPathProvider(VirtualPathProvider previous)
    {
        _previous = previous;
    }
    public override bool FileExists(string virtualPath)
    {
        if (IsEmbeddedPath(virtualPath))
            return true;
        else
            return _previous.FileExists(virtualPath);
    }
    public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
    {
        if (IsEmbeddedPath(virtualPath))
        {
            return null;
        }
        else
        {
            return _previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
        }
    }
    public override VirtualDirectory GetDirectory(string virtualDir)
    {
        return _previous.GetDirectory(virtualDir);
    }
    public override bool DirectoryExists(string virtualDir)
    {
        return _previous.DirectoryExists(virtualDir);
    }
    public override VirtualFile GetFile(string virtualPath)
    {
        if (IsEmbeddedPath(virtualPath))
        {
            string fileNameWithExtension = virtualPath.Replace("~/", string.Empty).Replace("/", ".").Replace("\\", ".").TrimStart(new char[] { '.' });
            string nameSpace = "myApp";
            string manifestResourceName = string.Format("{0}.{1}", nameSpace, fileNameWithExtension);
            Stream stream = null;

            foreach(var assembly in AppDomain.CurrentDomain.GetAssemblies().Where(a => a.FullName.StartsWith("myApp")).ToList())
            {
                stream = assembly.GetManifestResourceStream(manifestResourceName);
                if (stream != null)
                    break;
            }
            return new EmbeddedVirtualFile(virtualPath, stream ?? new MemoryStream());
        }
        else
            return _previous.GetFile(virtualPath);
    }
    private bool IsEmbeddedPath(string path)
    {
        return path.Contains("Scripts") || path.Contains("Content") || path.Contains("bundles") || path.Contains("Fonts") || path.Contains("Gfx");
    }
}
public class EmbeddedVirtualFile : VirtualFile
{
    private Stream _stream;
    public EmbeddedVirtualFile(string virtualPath, Stream stream)
        : base(virtualPath)
    {
        _stream = stream;
    }
    public override Stream Open()
    {
        return _stream;
    }
}

And last step I prepare Global.asax:

    public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        ConfigureBundles();
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

    private static void ConfigureBundles()
    {
        BundleTable.EnableOptimizations = true;
        BundleTable.VirtualPathProvider = new EmbeddedVirtualPathProvider(HostingEnvironment.VirtualPathProvider);
    }
}

All my css file and js file have set Build Action on Embedded Resource and this works very well in my main project.

What must I do if I want static files to load from external assembly into main project in the same way?

Thanks for help !!

RickL
  • 3,318
  • 10
  • 38
  • 39
Perdido
  • 238
  • 3
  • 12

0 Answers0