【问题标题】:Why is resource bundling being redirected in my ASP MVC4 app?为什么在我的 ASP MVC4 应用程序中重定向资源捆绑?
【发布时间】:2012-06-27 21:19:23
【问题描述】:

我正在使用 RC,并通过 NuGet 检查了所有内容是否都是最新的。 在我的 global.asax.cs 中,我得到了:

BundleTable.Bundles.AddDefaultFileExtensionReplacements();
BundleTable.Bundles.AddDefaultIgnorePatterns();
BundleTable.Bundles.AddDefaultFileOrderings();

Bundle scripts = new Bundle("~/Scripts");
scripts.IncludeDirectory("~/Scripts", "*.js");
BundleTable.Bundles.Add(scripts);

Bundle css = new Bundle("~/Content/css");
css.IncludeDirectory("~/Content/css", "*.css", false);
BundleTable.Bundles.Add(css);

我已经尝试了几种不同的配置,但没有任何改进。

然后在我的布局中我得到了:

<link href="@BundleTable.Bundles.ResolveBundleUrl("~/Content/css")" rel="stylesheet" type="text/css" />
<script src="@BundleTable.Bundles.ResolveBundleUrl("~/Scripts")"> </script>

当页面加载时,它的网址看起来不错:

<link href="/Content/css?v=QAsFYXHCbnaU70oGVxpgi9py9iKQrT9C4BVNdHa7xoI1" rel="stylesheet" type="text/css" />

但该网址重定向到:

/Content/css/

返回 404 not found 错误...

有人有什么想法吗?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 resourcebundle asp.net-optimization


    【解决方案1】:

    ~/Scripts 和 ~/Content/css 虚拟路径已经存在于磁盘上,因此您需要为它们设置一些虚拟 URL,比如说 ~/Scripts/js 和 ~/Content/styles 就是这样,它是现在好了。

    Bundle scripts = new Bundle("~/Scripts/js");
    scripts.IncludeDirectory("~/Scripts", "*.js");
    BundleTable.Bundles.Add(scripts);
    
    Bundle css = new Bundle("~/Content/styles");
    css.IncludeDirectory("~/Content/css", "*.css", false);
    BundleTable.Bundles.Add(css);
    

    同样在 MVC4 中,路由、捆绑包和过滤器配置已移至

    ~/App_Start/(RouteConfig, BundleConfig, FilterConfig).cs

    所以检查你有那些,如果有,然后在那里写你的配置。

    【讨论】:

    • 看起来是虚拟路径。我确定我已经看到了一些使用这些路径的示例!
    【解决方案2】:

    决定是否处理请求的捆绑模块逻辑不会接管对现有文件或目录的请求。因此,当您的捆绑请求与现有目录(或文件)位于相同的虚拟路径时,这就是为什么您的捆绑请求不起作用的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-01
      • 2020-12-27
      • 1970-01-01
      • 2012-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多