Sam*_*Sam 4 asp.net-mvc asp.net-mvc-4 bundling-and-minification asp.net-optimization
根据Scott Guthrie早些时候读过的一篇文章和Mads Kristensen发布的视频,我应该能够通过替换它来自动捆绑/缩小ASP.net MVC 4:
<link href="Styles/reset.css" rel="stylesheet" type="text/css" />
<link href="Styles/normalize.css" rel="stylesheet" type="text/css" />
<link href="Styles/styles.css" rel="stylesheet" type="text/css" />
<script src="Scripts/jquery-1.8.0.min.js" type="text/javascript"></script>
<script src="Scripts/jquery-validation.min.js" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)
有了这个:
<link href="Styles/css" rel="stylesheet" type="text/css" />
<script src="Scripts/js" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)
我尝试过针对.Net 4.0和4.5,它似乎没有什么区别.我收到404错误,链接和脚本标记从不指向捆绑的资源.
这个功能是否从最终版本中删除了?
我想将此功能用于"主题".
这就是我最终实现的方式.希望它有意义......
/// <summary>
/// Render stylesheets HTML for the given theme. Utilizes System.Web.Optimization for bundling and minification
/// </summary>
/// <param name="themeName">Name of theme</param>
/// <returns>HtmlString containing link elements</returns>
public static IHtmlString RenderThemeStyles(string themeName)
{
IHtmlString retValue = null;
// If no theme name is passed, return null
if (!themeName.HasValue())
return retValue;
var ctxt = HttpContext.Current;
string themePath = "~/Themes/" + themeName;
string themeKey = themePath + "/css";
if (ctxt.Cache[themeKey] != null)
return (IHtmlString)ctxt.Cache[themeKey];
// Check to see if the theme directory exists. Throw error if it does not
string themeSysPath = HttpContext.Current.Server.MapPath(themePath);
DirectoryInfo themeDir = new DirectoryInfo(themeSysPath);
if (!themeDir.Exists)
throw new ApplicationException(string.Format("Theme directory \"{0}\" does not exist", themePath));
// Remove the old bundle if it already exists
var old_bundle = BundleTable.Bundles.FirstOrDefault(b => b.Path == themeKey);
if (old_bundle != null)
BundleTable.Bundles.Remove(old_bundle);
if (themeDir.GetFiles("*.css").Length > 0)
{
// If there are css files, add them to the bundler and save the rendered output to cache
Bundle styles = new StyleBundle(themeKey).IncludeDirectory(themePath, "*.css");
BundleTable.Bundles.Add(styles);
retValue = Styles.Render(themeKey);
ctxt.Cache.Insert(themeKey, retValue, new System.Web.Caching.CacheDependency(themeSysPath));
}
else
{
// If there are no css files, save empty string to cache
ctxt.Cache.Insert(themeKey, new HtmlString(string.Empty), new System.Web.Caching.CacheDependency(themeSysPath));
}
return retValue;
}
Run Code Online (Sandbox Code Playgroud)
是的,虽然我找不到RC发行说明,但MVC4 RC版本中删除了此功能.
Rick Anderson关于将MVC4 beta升级到RC的博客文章描述了这个过程:
删除"自动捆绑"引用并BundleConfig.cs使用捆绑配置创建/复制a 并从Global.asax中调用它BundleConfig.RegisterBundles(BundleTable.Bundles);.
汉塞尔曼提到了一些有关决定的背景信息:
自beta以来,Web优化(缩小和捆绑)框架发生了一些重大变化.没有足够的控制捆绑的内容以及测试版中的顺序,因此已经移动到BundleConfig.cs(或.vb)中,您可以完全控制
不完全是,所以我们删除EnableDefaultBundles了你所谓的"自动捆绑",但基础功能仍然存在.
您可以通过注册来执行与该方法相同的操作:
BundleTable.Bundles.Add(new DynamicFolderBundle("js", "*.js");
BundleTable.Bundles.Add(new DynamicFolderBundle("css", "*.css");
Run Code Online (Sandbox Code Playgroud)
我们删除了该方法,因为这是一个相当有问题的方法,因为字母排序通常不是所需的排序.