title: ASP.NET Bundling/Minification and Embedded Resources
body: <p>I'm trying to use the technique described in <a href="http://weblogs.asp.net/imranbaloch/archive/2012/12/29/asp-net-bundling-and-minification-and-embedded-resources.aspx" rel="noreferrer">this blog</a> to add embedded dll resources to my bundles.</p>
  
  <p>I have created the custom <code>VirtualPathProvider</code> below.</p>
  
  <pre><code>public class EmbeddedVirtualPathProvider : VirtualPathProvider {
  
      private Type _rootType;
  
      public EmbeddedVirtualPathProvider(Type rootType) {
          _rootType = rootType;
      }
  
      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.Substring(virtualPath.LastIndexOf("/") + 1);
  
              string nameSpace = _rootType.Namespace;
              string manifestResourceName = String.Format("{0}.{1}", nameSpace, fileNameWithExtension);
              var stream = _rootType.Assembly.GetManifestResourceStream(manifestResourceName);
              return new EmbeddedVirtualFile(virtualPath, stream);
          }
          else
              return Previous.GetFile(virtualPath);
      }
  
      private bool IsEmbeddedPath(string path) {
          return path.Contains("~/Embedded");
      }
  }
  
  public class EmbeddedVirtualFile : VirtualFile {
      private Stream _stream;
      public EmbeddedVirtualFile(string virtualPath, Stream stream)
          : base(virtualPath) {
          _stream = stream;
      }
  
      public override Stream Open() {
          return _stream;
      }
  } 
  </code></pre>
  
  <p>I then register this and set up the bundles;</p>
  
  <pre><code>public static void RegisterBundles(BundleCollection bundles) {
  
      HostingEnvironment.RegisterVirtualPathProvider(new EmbeddedVirtualPathProvider(typeof(My.Custom.Type)));
  
      bundles.Add(new StyleBundle("~/Embedded/css").Include(
          "~/Embedded/files/styles/etc/styles.css")
      );
  }
  </code></pre>
  
  <p>I have also implemented a custom <code>EmbeddedResourceHttpHandler</code> as described in the article which works when requesting the file as a direct http request.</p>
  
  <p><strong>Problem:</strong>
  The embedded files aren't being included in the bundle, <em>they're just ignored</em>. When debugging the <code>FileExists</code> <em>method is called several times but never for</em> <code>~/Embedded/files/styles/etc/styles.css</code></p>
  
  <p>What am I missing?</p>
  
  <p><strong>Secondary Problem</strong></p>
  
  <p>When using the latest version of the Microsoft ASP.NET Web Optimization Framework. The <code>VirtualPathProvider</code> works as expected, however it prevents the <code>IRouteHandler</code> from executing. If the <code>FileExists</code> method is changed to return false this allows the RouteHandler to execute but obviously breaks the <code>VirtualPathProvider</code>.</p>
  
  <p>I'm assuming it's not using a configured route because it's looking for a physical file when <code>FileExists</code> returns true? Is this a configuration or an implementation issue?</p>