asp.net - BundleTable.Bundles.GetBundleFor() 返回但不返回 Items inside

标签 asp.net asp.net-mvc bundling-and-minification

简要说明:

包含来自 VirtualPath(由自定义 VirtualPathProvider 提供服务)的项目的 bundle 不起作用。哈希未在 v= 中生成,解析它们显示 .Items 属性为空。

详细:

假设我们有一个包注册:

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

这里有一些注意事项:

  1. ~/bundles/admin/somestyle 物理上不存在。
  2. ~/Areas/Admin/Content/css/aaa2.css 是一个虚拟路径,由自定义 VirtualPathProvider
  3. 处理
  4. ~/bundles/css 默认提供 MapPathBasedVirtualPathProvider

当前Web.config文件的相关部分:

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

路由条目:

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

试驾:

1。尝试直接访问 .css 文件:

http://localhost:1010/Areas/Admin/Content/css/aaa2.css - 找到文件 => 自定义 VirtualPathProvider 完成了他的工作。

2。尝试从 HomeController 解析 bundles:

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 已找到,但 .Items 属性为空。
  • bundleVirtualUrl/bundles/admin/somestyle?v= 注意 v= 没有哈希值。
  • bundleOrdinary 找到,.Items 有一个条目 ~/Content/site.css
  • bundleOrdinaryUrl"/bundles/css?v=6bMbVqKBetPV3UISUSkpqR5wiBsbzK_c6J21LUZAzaU1"

问题:

为什么解析“~/bundles/admin/somestyle”显示没有添加项目,即使项目已注册并且 VirtualPathProvider 正确处理它?<​​/p>

相关链接:

  1. ASP.NET Bundling/Minification and Embedded Resources
  2. http://www.codeproject.com/Articles/728146/ASP-NET-MVC-bundles-internals

最佳答案

您需要在捆绑注册之前注册您的 VirtualPathProvider,以便能够找到您的文件。任何找不到的文件都不会包含在 bundle.Items 中

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

当然,除非您已经将其注册为 HostingEnvironment.VirtualPathProvider。

关于asp.net - BundleTable.Bundles.GetBundleFor() 返回但不返回 Items inside,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28499885/

相关文章:

asp.net - 安装 MvcScaffolding 包时出现 NuGet 执行策略错误

c# - 在 asp.net mvc Twitter OAuth2 登录后获取 oauth 凭据

asp.net-mvc - 使用 Razor 的 ASP.NET MVC 部分渲染问题

html - 如何在 Release 模式下自动删除 HTML 注释?

javascript - 如何使用 RenderAction 将数据从一个 View 传递到另一个 View

asp.net - 以编程方式完成 ADFS 登录

asp.net - 如何将动态加载的 webforms 用户控件转换为 mvc razor

asp.net - ASP.NET MVC 中过去的 Cookie 过期日期

css - 使用 Webpack,是否可以只生成 CSS,不包括 output.js?

ASP.NET 捆绑 - 忽略第二个 JS 文件