加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 自动捆绑/缩小是否已在最终版本的MVC 4中获得?

发布时间:2020-12-16 07:32:19 所属栏目:asp.Net 来源:网络整理
导读:根据 article I read earlier by Scott Guthrie和 video posted by Mads Kristensen,我应该能够通过替换以下内容在ASP.net MVC 4中自动捆绑/缩小: link href="Styles/reset.css" rel="stylesheet" type="text/css" /link href="Styles/normalize.css" rel="
根据 article I read earlier by Scott Guthrie和 video posted by 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>

有了这个:

<link href="Styles/css" rel="stylesheet" type="text/css" />

<script src="Scripts/js" type="text/javascript"></script>

我尝试过针对.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;
 }

解决方法

是的,虽然我找不到RC发行说明,但MVC4 RC版本中删除了此功能.

Rick Anderson在upgrade MVC4 beta to RC的博客文章描述了这个过程:

删除“自动捆绑”引用并使用bundle configs创建/复制BundleConfig.cs,并使用BundleConfig.RegisterBundles(BundleTable.Bundles);从Global.asax调用它.

汉塞尔曼提到了一些关于这个决定的backround info:

There’s been some significant changes to the web optimization (minification and bundling) framework since beta. There wasn’t enough control over what was bundled and in what order in the beta,so that’s been moved into a BundleConfig.cs (or .vb) where you have total control

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读