使用 ASP.NET MVC4 应用程序 MVC4 buildit 捆绑和缩小。 应用程序也应该可以离线工作。
我尝试使用下面的代码创建 list ,但这不起作用,因为 v= 参数将更改 如果源代码被更改。 如果使用捆绑和缩小,如何创建无需手动更改捆绑查询字符串参数即可工作的 list ?
<!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>
Controller :
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 进入 Debug模式
我尝试使用答案中的建议
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")),
... 在 Controller 中。
在浏览器中查看源代码
<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>
生成的 list 包含
CACHE MANIFEST
CACHE:
/erp/Sale/pos.js
/erp/Sale/css/pos
/erp/Sale/css/receipt
如何解决这个问题? 看起来生成的 list 无效网址:
- 已添加销售目录
- 查询字符串不存在
最佳答案
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"))
};
}
}
}
仅当站点使用 <compilation debug="false" />
运行时才会添加缓存破坏哈希值。在 web.config 文件中指定。
关于asp.net - 如果在 ASP.NET MVC4 中使用捆绑和缩小,如何创建 list ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20600245/