【问题标题】:how to create manifest if bundling and minification is used in ASP.NET MVC4如果在 ASP.NET MVC4 中使用捆绑和缩小,如何创建清单
【发布时间】:2014-01-03 05:07:37
【问题描述】:

使用了 ASP.NET MVC4 应用程序 MVC4 buildit 捆绑和缩小。 应用程序也应离线工作。

我尝试使用下面的代码创建清单,但这不起作用,因为 v= 参数会改变 如果源代码被更改。 如果使用捆绑和缩小,如何创建无需手动更改捆绑查询字符串参数即可工作的清单?

<!DOCTYPE html>
<html manifest="~/Sale/Manifest">

<head>

    @Erp.Helpers.Bundles.Render("~/css/pos", new { media = "screen" })
    @Erp.Helpers.Bundles.Render("~/css/receipt", new { media = "print" })
    @Scripts.Render("~/pos.js")
</head>

控制器:

public ContentResult Manifest()
        {
            return new ContentResult()
            {
                Content = @"CACHE MANIFEST
CACHE:
pos?v=esdw2223023094323
css/pos?v=ewe3334324r33432343
css/receipt?v=w333543334324r33432343

NETWORK:
*
"
   };
}

C# Web.Optimization Bundles and HTML5 cache Manifest 包含类似的问题但没有答案。

更新

我尝试了答案,但在 chrome 控制台中出现错误

Application Cache Error event: Resource fetch failed (404) http://localhost:52216/erp/Sale/pos.js 

应用程序正在运行

        BundleTable.EnableOptimizations = true;

在 VSE 2013 for Web 中按 F5 进入调试模式

我在答案中尝试了使用

的建议
public class SaleController : ControllerBase
{

    public ContentResult Manifest()
    {
        return new ContentResult()
        {
            Content = string.Format(@"CACHE MANIFEST
CACHE:
   {0}
   {1}
   {2}
", 
Scripts.Url("pos.js"),  // tried also Scripts.Url("pos")
Styles.Url("css/pos"),
Styles.Url("css/receipt")),

... 在控制器中。

在浏览器中查看源码

<html manifest="/erp/Sale/Manifest">
<head>
  <link href="/erp/css/pos?v=sN8iz_u3gRGE6MQKGq6KocU-SUIUMcQxKVuESa_L2l41" rel="stylesheet" media="screen"/>
    <link href="/erp/css/receipt?v=C6qCUe6W1VM6yxNe-RgZRY9R0Gws1vU0lMuoKIn1uVE1" rel="stylesheet" media="print"/>
    <script src="/erp/pos.js?v=a-TsureIIlzxwhaItWMV7Ajfw-YXE3x6hUld3cxwAqI1"></script>

生成的清单包含

CACHE MANIFEST
CACHE:
/erp/Sale/pos.js
/erp/Sale/css/pos
/erp/Sale/css/receipt

如何解决这个问题? 似乎生成了无效的 url 来显示:

  1. 销售目录已添加
  2. 查询字符串不存在

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-4 bundling-and-minification cache-manifest


    【解决方案1】:

    当使用debug="true" 运行时,您可以使用Scripts.RenderFormatStyles.RenderFormat 呈现文件的完整列表,而使用debug="false" 运行时仅呈现缩小版本。

    这是一个例子:

        public ActionResult AppManifest()
        {
            string cacheFiles = "";
    
            cacheFiles = Scripts.RenderFormat("{0}",
                "~/bundles/jquery",
                "~/bundles/jqueryval",
                "~/bundles/bootstrap").ToString();
    
            cacheFiles += Styles.RenderFormat("{0}",
                "~/Content/css");
    
            string manifest = String.Format(@"CACHE MANIFEST
    
      CACHE:
      {0}", cacheFiles);
    
            return Content(manifest, "text/cache-manifest");
        }
    

    【讨论】:

      【解决方案2】:

      System.Web.Optimization 中的 Styles 和 Scripts 对象上有一些方法可以为您提供资源的 URL。

      您可以调用Scripts.Url(string) 为您提供脚本文件的URL,并附上哈希值。您也可以调用 Styles.Url(string) 为您提供样式表的 URL,并附上哈希。

      using System.Web.Optimization;
      using System.Web.Mvc;
      
      namespace StackOverflow.Controllers
      {
          public class StackOverflowController : Controller
          {
              public ContentResult Manifest()
              {
                  return new ContentResult()
                  {
                      Content = string.Format( 
      @"CACHE MANIFEST
      CACHE:
      {0}
      {1}
      {2}
      
      NETWORK:
      *
      ",
       Scripts.Url("~/pos.js"),
       Styles.Url("~/css/pos"),
       Styles.Url("~/css/receipt"))
                  };
              }
          }
      }
      

      只有在使用 web.config 文件中指定的&lt;compilation debug="false" /&gt; 运行网站时才会添加缓存清除哈希。

      【讨论】:

      • 谢谢。此示例不使用完整的捆绑包名称。应该使用 ~/pos.js 代替 pos 吗?在调试模式下,捆绑被关闭并引用了多个文件。如何在不捆绑的情况下自动完成这项工作?
      • Pervez Choudhury:我试过这个,但得到清单提取错误。我更新了问题。如何解决?
      • @Andrus - 我已将相对 URL 添加到代码中,并添加了有关查询字符串参数何时可见的注释。
      • Bundle 包含许多 css 和 js 文件。您的解决方案仅呈现单个文件,因此它无法在调试模式下工作。我应该使用kazimanzurrashid.com/posts/… 的解决方案吗?看起来这项工作在调试模式下也是如此吗?
      【解决方案3】:

      Scripts.Url 为您提供捆绑包的 URL,而不呈现脚本标签。这能回答你的问题吗?

      【讨论】:

      • 我试过这个,但得到清单提取错误。我更新了问题。如何解决?
      • 你把同样的东西传递给 Scripts.Url 就像你传递给 Scripts.Render - 即你的包的名字。在上面你有“~/pos.js”作为你的包的名字。
      猜你喜欢
      • 2012-06-28
      • 1970-01-01
      • 2013-09-11
      • 1970-01-01
      • 2013-02-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多