BundleTable.Bundles.GetBundleFor() returns but not Items inside
Asked Answered
O

1

3

Briefly:

Bundles which contain items from a VirtualPath (Served by custom VirtualPathProvider) don't work. Hash isn't generated in the v= and resolving them shows the .Items property as empty.

Detailed:

Suppose we have a bundle registration:

  bundles.Clear();
  bundles.ResetAll();
  BundleTable.EnableOptimizations = true;
  bundles.Add(new StyleBundle("~/bundles/css")
           .Include("~/Content/site.css"));
  bundles.Add(new StyleBundle("~/bundles/admin/somestyle")
           .Include("~/Areas/Admin/Content/css/aaa2.css"));

Some notes here:

  1. ~/bundles/admin/somestyle physically doesn't exist.
  2. ~/Areas/Admin/Content/css/aaa2.css is a virtual path which is handled by a custom VirtualPathProvider
  3. ~/bundles/css is served by default MapPathBasedVirtualPathProvider

Relevant part of current Web.config file:

<system.webServer>
  <validation validateIntegratedModeConfiguration="false" />
  <modules runAllManagedModulesForAllRequests="false"/>
  <handlers>
     <remove name="OPTIONSVerbHandler" />
     <remove name="TRACEVerbHandler" />
     <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
     <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
     <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
     <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
     <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
     <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

     <add name="AspNetStaticFileHandler-TIFF" path="*.tiff" verb="*" type="System.Web.StaticFileHandler" />
     <add name="AspNetStaticFileHandler-GIF"  path="*.gif"  verb="*" type="System.Web.StaticFileHandler" />
     <add name="AspNetStaticFileHandler-PNG"  path="*.png"  verb="*" type="System.Web.StaticFileHandler" />
     <add name="AspNetStaticFileHandler-JPG"  path="*.jpg"  verb="*" type="System.Web.StaticFileHandler" />
     <add name="AspNetStaticFileHandler-CSS"  path="*.css"  verb="*" type="System.Web.StaticFileHandler" />
     <add name="AspNetStaticFileHandler-JS"   path="*.js"   verb="*" type="System.Web.StaticFileHandler" />
     <add name="AspNetStaticFileHandler-WOFF" path="*.woff" verb="*" type="System.Web.StaticFileHandler" />
     <add name="AspNetStaticFileHandler-TTF"  path="*.ttf"  verb="*" type="System.Web.StaticFileHandler" />
     <add name="AspNetStaticFileHandler-MAP"  path="*.map"  verb="*" type="System.Web.StaticFileHandler" />
   </handlers>
 </system.webServer>

Routing entries:

 routes.IgnoreRoute("favicon.ico");
 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 AreaRegistration.RegisterAllAreas();
 RouteConfig.RegisterRoutes(RouteTable.Routes);

Test Drive:

1. Trying to access the .css files directly:

http://localhost:1010/Areas/Admin/Content/css/aaa2.css - File found => Custom VirtualPathProvider did his job.

2. Trying to resolve bundles from HomeController:

public ActionResult Test()
{
    var bundleVirtual = BundleTable.Bundles.GetBundleFor("~/bundles/admin/somestyle");
    var bundleVirtualUrl = BundleTable.Bundles.ResolveBundleUrl("~/bundles/admin/somestyle");

    var bundleOrdinary = BundleTable.Bundles.GetBundleFor("~/bundles/css");
    var bundleOrdinaryUrl = BundleTable.Bundles.ResolveBundleUrl("~/bundles/css");

    return View();
 }
  • bundleVirtual is found but .Items property is empty.
  • bundleVirtualUrl is /bundles/admin/somestyle?v= notice that v= has no hash value.
  • bundleOrdinary is found, .Items has one entry ~/Content/site.css.
  • bundleOrdinaryUrl is "/bundles/css?v=6bMbVqKBetPV3UISUSkpqR5wiBsbzK_c6J21LUZAzaU1"

Question:

Why resolving "~/bundles/admin/somestyle" shows that there are not Items added, even if the item is registered and the VirtualPathProvider handles it correctly?

Related links:

  1. ASP.NET Bundling/Minification and Embedded Resources
  2. http://www.codeproject.com/Articles/728146/ASP-NET-MVC-bundles-internals
Ole answered 13/2, 2015 at 12:43 Comment(0)
E
2

You need to register your VirtualPathProvider before bundle registration, in order to be able to find your files. Any files not found will not be included in bundle.Items

var vpp = new MyVirtualPathProvider();
BundleTable.VirtualPathProvider = vpp;
BundleConfig.RegisterBundles(BundleTable.Bundles);

Unless of course you already have it registered as HostingEnvironment.VirtualPathProvider.

Extender answered 13/2, 2015 at 16:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.