【问题标题】:ASP MVC - Bundles creating directory path instead of file pathASP MVC - 捆绑创建目录路径而不是文件路径
【发布时间】:2013-02-24 04:47:58
【问题描述】:

我有这个:

bundles.Add(new StyleBundle("~/Content/Styles/Default").Include("~/Content/Styles/Default/Site.css"));

它创建了这个:

<link href="/Content/Styles/Default?v=HG5hShy6_NaqI7SUDWQuc6zijexRxZooKF4ayIgK5tY1" rel="stylesheet">

现在,我在我的网络服务器上启用了目录浏览,当我单击该样式路径时,它会将我移动到一个目录,而不是一个文件!为什么?

更新:我还是没能解决这个问题,当我打开链接http://myserver/Content/Styles/Default?v=HG5hShy6_NaqI7SUDWQuc6zijexRxZooKF4ayIgK5tY1 时,我得到的只是一个文件列表(比如在 ftp 上)

【问题讨论】:

  • 试试这个 bundles.Add(new StyleBundle("~/Content/Styles/Default/Site.css")
  • 但是如果我想添加多个文件呢?
  • @Kundan Singh Chouhan:我刚刚尝试了您的解决方案,我从中得到的只是:&lt;link href="/Content/Styles/Default" rel="stylesheet"&gt;。这是怎么回事……

标签: asp.net asp.net-mvc asp.net-mvc-3


【解决方案1】:

先看看这个article

捆绑是 ASP.NET 4.5 中的一项新功能,可以轻松组合 或将多个文件捆绑到一个文件中。您可以创建 CSS, JavaScript 和其他捆绑包。更少的文件意味着更少的 HTTP 请求 这可以提高首页加载性能。

请求

http://localhost/MvcBM_time/bundles/AllMyScripts?v=r0sLDicvP58AIXN_mc3QdyVvVj5euZNzdsa2N1PKvb81

用于捆绑包 AllMyScripts 并包含一个查询字符串对 v=r0sLDicvP58AIXN_mc3QdyVvVj5euZNzdsa2N1PKvb81。

查询字符串 v 有 一个值标记,它是用于缓存的唯一标识符。只要 捆绑包没有改变,ASP.NET 应用程序将请求 AllMyScripts 捆绑使用此令牌。如果包中的任何文件 更改,ASP.NET 优化框架将生成一个新的令牌, 保证对捆绑包的浏览器请求将获得最新的 捆绑

这是添加文件目录的方法

bundles.Add(new StyleBundle("~/jQueryUI/themes/baseAll")
    .IncludeDirectory("~/Content/themes/base", "*.css"));

这是添加多个文件的方法:

bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
          "~/Content/themes/base/jquery.ui.core.css",
          "~/Content/themes/base/jquery.ui.resizable.css",
          "~/Content/themes/base/jquery.ui.selectable.css",
          "~/Content/themes/base/jquery.ui.accordion.css",
          "~/Content/themes/base/jquery.ui.autocomplete.css",
          "~/Content/themes/base/jquery.ui.button.css",
          "~/Content/themes/base/jquery.ui.dialog.css",
          "~/Content/themes/base/jquery.ui.slider.css",
          "~/Content/themes/base/jquery.ui.tabs.css",
          "~/Content/themes/base/jquery.ui.datepicker.css",
          "~/Content/themes/base/jquery.ui.progressbar.css",
          "~/Content/themes/base/jquery.ui.theme.css"));

【讨论】:

  • 好吧,我添加了一个文件,它指向一个目录! ://
【解决方案2】:

这是一个老问题,但当我在寻找解决方案时,Google 指出了我这里,所以我想我会添加对我有用的东西。

我遇到了和你一样的问题,并且遇到了我认为相同的另一个问题:

MVC4 - Bundling does not work when optimizations are set to true

我想问题是你把包放在一个实际存在的虚拟 URL 上,但它是一个目录。

更改捆绑路径解决了我遇到的问题。

【讨论】:

    猜你喜欢
    • 2021-08-27
    • 1970-01-01
    • 2014-03-08
    • 2016-07-09
    • 2021-03-24
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    • 2012-03-20
    相关资源
    最近更新 更多