【问题标题】:Bundling with MVC4 not working after I publish to azure发布到天蓝色后,与 MVC4 捆绑不起作用
【发布时间】:2013-06-03 13:53:23
【问题描述】:

您好,我正在尝试为我的应用程序捆绑我的脚本。我的调试工作正常,如果我使用 Web.debug 发布,一切正常。但是当我使用 Web.releas 发布时,我的脚本不会加载。一切都在本地工作,只有当我从 VS2012 发布到 Azure 时它才会停止。以下是我创建捆绑包的方式。

namespace BAT.App_Start
{
  public class BundleConfig
  {
    public static void RegisterBundles(BundleCollection bundles)
    {
        //BundleTable.EnableOptimizations = true;

        bundles.Add(new ScriptBundle("~/Content/MasterCss")
                .Include("~/Content/bootstrap.css")
                .Include("~/Content/bootstrap-responsive.css")
                .Include("~/Content/CSS/site.css"));

        bundles.Add(new ScriptBundle("~/Scripts/MasterScripts")
                .Include("~/Scripts/jquery-{version}.js")
                .Include("~/Scripts/bootstrap.js"));

        bundles.Add(new ScriptBundle("~/Scripts/Validation")
                .Include("~/Scripts/jquery.validate.js")
                .Include("~/Scripts/jquery.validate.unobtrusive.js"));
        }
    }
}

未注释的行破坏了调试版本

这是我调用捆绑包的布局

@using System.Web.Optimization
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title Business Analysis Tool </title>

    @Styles.Render("~/Content/MasterCss")
</head>

<body>
    <div class="container-fluid">
        <div class="row-fluid"> @RenderPage("~/Views/Shared/_Header.cshtml") </div>
        <div class="row-fluid"> @RenderBody() </div>
        <div class="row-fluid"> @RenderPage("~/Views/Shared/_Footer.cshtml") </div>
    </div>
    @Scripts.Render("~/Scripts/MasterScripts")    
    @RenderSection("scriptholder", false)
</body>
</html>

这是我的 Release.Config

<?xml version="1.0"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> 
    <system.web>
        <compilation xdt:Transform="RemoveAttributes(debug)" />
    </system.web>
    </configuration>

这是我在页面上使用 CTRL+U 检查捆绑脚本时的错误链接 http://bat.azurewebsites.net/Content/MasterCss?v=htASNz4hgFFA40tt0CVZdpwQudN8ZW4429UjRQZQJms1

这似乎与缩小有关。我已经学习了一些教程并在这里阅读了其他帖子,但他们的解决方案对我不起作用

【问题讨论】:

  • 嗨,杰夫。我似乎处于同样的境地。您对此有任何反馈吗?
  • 嗨,不,我还没有运气。为了让某些东西正常工作,我使用调试版本发布到 Azure。这对我有用,因为我没有很多脚本,但我真的很想让捆绑工作
  • 好的,如果我有什么想法会告诉你
  • 我也看到了同样的问题。如果我发布调试配置文件,它不会捆绑和工作。如果我发布发布配置文件,我页面上的 jquery 脚本似乎不再工作。

标签: asp.net-mvc-4 azure bundle


【解决方案1】:

这个问题真的很简单:

我有以下:

bundles.Add(new StyleBundle("~/Content/css").Include(
                  "~/Content/bootstrap.css",
                  "~/Content/site.css"));

我的解决方案中还有一个文件夹:

/Content/Css

这就是问题所在,stylebundle 与我的解决方案中的文件夹同名。

将样式包重命名为:

 bundles.Add(new StyleBundle("~/scripts/css").Include(
                  "~/Content/bootstrap.css",
                  "~/Content/site.css"));

并且(记住)改变你引用它的位置,_Layout.cshmtl

因此,为了明确这一点,如果您的 stylebundle 名称与解决方案中的实际文件夹相同,那么您将遇到此问题。 只需将其命名为不同的名称即可。

【讨论】:

  • 类似的问题。有一个文件夹 Scirpts/app 并且在 BundleConfig 中定义了一个包:~/bundles/app。我的脚本位于 app 文件夹中。将 BundleConfig.cs 中的 ~/bundles/app 重命名为 ~/bundles/theapp 后,它就可以工作了。
【解决方案2】:

小错误杀死了我,我将我的 css 捆绑为脚本包而不是样式包。

我做了很多尝试来解决这个问题,包括设置源代码控制和构建配置。我的项目从设置使用 git 到使用 tfs 等等。

【讨论】:

    【解决方案3】:

    在 BundleConfig.cs 我有以下内容:

            bundles.Add(new StyleBundle("~/Content/css").Include(
                "~/Content/css/bootstrap.css",
                "~/Content/css/bootstrap-responsive.css",
                "~/Content/css/site.css"));
    

    我真的不得不使用:

    bundles.Add(new StyleBundle("~/Styles/css").Include(
                "~/Content/css/bootstrap.css",
                "~/Content/css/bootstrap-responsive.css",
                "~/Content/css/site.css"));
    

    我还必须在 _Layout.cshtml 中更新对我的 CSS 样式包的引用

    @Styles.Render("~/Styles/css")
    

    我在这里找到了这个答案:http://thebeardeveloper.blogspot.com.au/2013/02/403-forbidden-for-css-bundles-in-asp.html

    【讨论】:

    • 这是对我有帮助的答案。
    【解决方案4】:

    我遇到了同样的问题,但以上解决方案都不适合我。

    问题是,我有这样的捆绑:

    @Styles.Render("~/Content/css")
    
    bundles.Add(new StyleBundle("~/Content/css").Include(
        "~/Content/bootstrap.css",
        "~/Content/justified-nav.css",
        "~/Content/site.css"));
    

    但是,~/Content/css 也是一个目录 - 在部署到 Azure 时损坏了。我通过在两个位置将 ~/Content/css 更改为 ~/Content/mastercss 来解决此问题

    【讨论】:

      【解决方案5】:

      我在发布到 Azure 时遇到了类似的问题。该错误实际上是告诉我 jQuery 文件格式错误。 ==> 缩小的文件。

      其他脚本失败,因为 jQuery 未被识别。

      我使用的是 jQuery 2.0.0(来自 NuGet)。今天升级到 2.0.2(从 NuGet)后,我能够在发布配置下成功发布到 Azure。

      不确定这是否与您的具体问题有关,但它解决了我的问题。

      【讨论】:

      • 很抱歉延迟尝试这个,但这对我不起作用。不过谢谢回复
      【解决方案6】:

      在我缺少样式表的情况下,问题不是由于发布到 Azure 的变化无常、缩小过程中的失败或虚拟路径和真实路径冲突。

      这是由于BundleCollection.Add(Bundle)DebugRelease之间的行为不同。

      如果您设法执行以下操作(例如,因为您使用 NuGet 中的 Durandal 并且 WebActivator 初始化了多个 BundleConfig,而您自己并没有真正编写它们)

      bundles.Add(new StyleBundle("/foo").Include("/a.css")); 
      bundles.Add(new StyleBundle("/foo").Include("/b.css"));
      

      基本上,如果捆绑包没有缩小,则可以这样做:您可以在 Razor 页面中获得 /foo/a.css/foo/b.css 的元素。

      但如果缩小参与,则b.css 不会进入生成的缩小资源。 (或者 a.css 被替换了……我没注意……只是想让它工作)。

      您可以通过了解加载和使用顺序来解决此问题:

      bundles.Add(new StyleBundle("/foo").Include("/a.css")); 
      bundles.Add(bundles.GetBundleFor("/foo").Include("/b.css"));
      

      ... 或使用不同的命名路径(如果这确实是您的意图),或改进您的策略以捆绑初始化(这可能会破坏 NuGet 更新)。

      如果只是 System.Web.Optimization 类的愚蠢“文档”实际上费心指定添加具有相同虚拟路径的多个捆绑包的行为,我们就不必进行此讨论了。

      【讨论】:

        【解决方案7】:

        让我们挖一些坟墓。 今天我开始摆弄 Azure,很快就遇到了类似的问题。

        样式包的定义如下:

        bundles.Add(new StyleBundle("~/Styles/css").Include(
                          "~/Content/bootstrap.superhero.css",
                          "~/Content/site.css"));
        

        但是没有渲染 css。 在几乎打开第二个问题后,我意识到文件 bootstrap.superhero.css 在解决方案资源管理器中显示为灰色。在 Visual Studio 中本地运行的内容在 Azure 中失败,因为该文件尚未在项目中实现。

        Tl;dr:右键单击 Css 文件,然后“包含在项目中”

        【讨论】:

          【解决方案8】:

          作为提到的答案之一,我已经为此奋斗了几个小时,我的问题还在于我有一个名为 scripts 的文件夹,所以我将脚本的名称从(这是来自我的索引视图)更改了>

          @Scripts.Render("~/Scripts/CircleStats") //this is also a folder in my project, which I think is the problem for azure
          

          @Scripts.Render("~/bundles/CircleStats")
          

          它正在工作,希望这会对某人有所帮助

          https://forums.asp.net/t/1810635.aspx?403+Access+denied+when+using+Bundling

          【讨论】:

            【解决方案9】:

            在我的例子中,我的包的虚拟路径包含. 字符。类似的东西:

            bundles.Add(new ScriptBundle("~/bundles/jquery.loadTemplate").Include(
                        "~/Scripts/jquery.loadTemplate/*.js");
            

            我用连字符改变了点:

            bundles.Add(new ScriptBundle("~/bundles/jquery-loadTemplate").Include(
                        "~/Scripts/jquery-loadTemplate/*.js");
            

            一切都像魅力一样。

            我仍然不确定为什么它可以在我的本地机器上运行,但不能在 Azure 上运行...

            【讨论】:

              猜你喜欢
              • 2013-07-15
              • 2015-07-21
              • 2013-01-14
              • 2020-05-05
              • 1970-01-01
              • 1970-01-01
              • 2013-04-02
              • 2016-07-07
              • 1970-01-01
              相关资源
              最近更新 更多