ASP.NET MVC 4捆绑 - 在调试模式下单个文件的网址 [英] ASP.NET MVC 4 Bundling - Individual File URLs in DEBUG mode
问题描述
问题:
在HTML5离线应用程序正在ASP.NET MVC 4.5做的,我们是捆绑和污染减量使用框架的的内置功能。一切都与网页以及工作本身,而是写入到缓存清单,其中(在我们,因为我们正在编写它),它总是只发射捆绑的URL。
因此,我们不能够在离线模式下调试JavaScript,作为单独的调试JS文件不会进入应用程序缓存。
code:
RegisterBundles
这是怎么了 BundleConfig.RegisterBundles
的外观:
//关于捆绑的更多信息,请访问http://go.microsoft.com/fwlink/?LinkId=254725
公共静态无效RegisterBundles(BundleCollection包)
{
bundles.Add(新ScriptBundle(〜/包/脚本)。包括(
〜/脚本/ *。JS
));
}
HTML标记
和我们包括在我们的 _Layout.cshtml
像这样的网页本身:
@ System.Web.Optimization.Scripts.Render(〜/包/脚本)
这非常适用于网页,通过发射单个js文件时,调试
是真
,和一个捆绑式文件时,调试
是假
。
输出在 debug = true可
<脚本的src =/脚本/ ScriptOne.js>< / SCRIPT>
<脚本的src =/脚本/ ScriptTwo.js>< / SCRIPT>
<脚本的src =/脚本/ ScriptThree.js>< / SCRIPT>
输出在调试=假
<脚本的src =/包/脚本V = B0_RvAM_5ifnREcGnNQ3FO8qQp4vyLOdtCUJ-2mXSuA1?>< / SCRIPT>
缓存清单
这是我们包括脚本到我们的 CacheManifest
@ System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl(〜/包/脚本)
输出在 debug = true可
和调试=假
/包/脚本?V = B0_RvAM_5ifnREcGnNQ3FO8qQp4vyLOdtCUJ,2mXSuA1
我们要怎么办?
我们想知道是否有一种方法,我们可以得到的缓存清单是这样的输出:
输出在 debug = true可
/Scripts/ScriptOne.js
/Scripts/ScriptTwo.js
/Scripts/ScriptThree.js
输出在调试=假
/包/脚本?V = B0_RvAM_5ifnREcGnNQ3FO8qQp4vyLOdtCUJ,2mXSuA1
的关于某一 Scripts.RenderFormat
方法,它看起来像一个很好的候选人做什么,我们想要的MSDN文档会谈。但是,智能感知抱怨这个 RenderFormat
方法并不present在 System.Web.Optimization
的版本,目前引用
不过,感谢(再次)谷歌,这个答案在这里SO 中解释说,这个 RenderFormat
方法其实,列入下一个版本的alpha版本。而<一href="http://stackoverflow.com/questions/12865939/mvc4-bundling-minification-with-ie-conditional-comments#comment17608457_12923723">comment对这个问题的答案链接到该解释我们如何安装它的页面:
PM&GT;安装,包装Microsoft.AspNet.Web.Optimization - pre
在这个版本中,缓存清单
可以更改为:
@ System.Web.Optimization.Scripts.RenderFormat({0},〜/包/脚本)
和我们现在拥有的缓存清单发出单独的文件,而 debug = true可
。
显然,MSDN文档不符合当前稳定版本同步!
Problem:
In the HTML5 offline app being done in ASP.NET MVC 4.5, we are bundling and minifying the styles and scripts using the framework's built-in feature. Everything working well with the pages themselves, but for writing into the Cache Manifest, where (because of the we we are writing it) it is always only emitting the bundled URL.
And so, we are not able to debug JavaScript in the offline mode, as the individual debug js files are not getting into the application cache.
Code:
RegisterBundles
This is how our BundleConfig.RegisterBundles
look:
// For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/Scripts").Include(
"~/Scripts/*.js"
));
}
HTML Markup
And we include it in our _Layout.cshtml
for the pages itself like this:
@System.Web.Optimization.Scripts.Render("~/bundles/Scripts")
This works well for the pages, by emitting the individual js files when debug
is true
, and one bundled file when debug
is false
.
Output in debug=true
<script src="/Scripts/ScriptOne.js"></script>
<script src="/Scripts/ScriptTwo.js"></script>
<script src="/Scripts/ScriptThree.js"></script>
Output in debug=false
<script src="/bundles/Scripts?v=B0_RvAM_5ifnREcGnNQ3FO8qQp4vyLOdtCUJ-2mXSuA1"></script>
Cache-Manifest
And this is how we include the scripts into our CacheManifest
@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/bundles/Scripts")
Output in debug=true
and debug=false
/bundles/Scripts?v=B0_RvAM_5ifnREcGnNQ3FO8qQp4vyLOdtCUJ-2mXSuA1
What we want?
We would like to know if there is a way we could get the Cache-Manifest to be output like this:
Output in debug=true
/Scripts/ScriptOne.js
/Scripts/ScriptTwo.js
/Scripts/ScriptThree.js
Output in debug=false
/bundles/Scripts?v=B0_RvAM_5ifnREcGnNQ3FO8qQp4vyLOdtCUJ-2mXSuA1
The MSDN documentation talks about a certain Scripts.RenderFormat
method, which looked like a good candidate for doing what we want. But intellisense was complaining this RenderFormat
method is not present in the version of System.Web.Optimization
currently referenced.
But, thanks (once again) to Google, this answer here on SO was explaining that this RenderFormat
method is in fact, included in the next version's alpha release. And the comment on that answer linked to the page that explains how we could install it:
PM> Install-Package Microsoft.AspNet.Web.Optimization -Pre
With this version, the Cache-Manifest
could be changed to:
@System.Web.Optimization.Scripts.RenderFormat("{0}","~/bundles/Scripts")
And we now have the cache manifest emit the individual files while debug=true
.
Apparently, the MSDN documentation is not in sync with the current stable release !
这篇关于ASP.NET MVC 4捆绑 - 在调试模式下单个文件的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!