ASP.NET Web Forms中用System.Web.Optimization取代SquishIt

來源:互聯網
上載者:User

將一個ASP.NET Web Forms項目從.NET Framework 4.0升級至.NET Framework 4.5之後,發現SquishIt竟然引發了HTTP Error 500.0 - Internal Server Error。

SquishIt是一個開源的支援ASP.NET的js/css打包工具,項目地址:https://github.com/jetheredge/SquishIt,出生早於Microsoft ASP.NET Web Optimization Framework(System.Web.Optimization)。

由於SquishIt不支援.NET Framework 4.5,只能棄之改用System.Web.Optimization。

以下是在實際的ASP.NET Web Forms項目中使用System.Web.Optimization的操作步驟:

1. 通過NuGet添加Microsoft ASP.NET Web Optimization Framework的引用

2. 在App_Code中添加BundleConfig.cs

using System.Web.Optimization;    public class BundleConfig{    public static void RegisterBundles(BundleCollection bundles)    {        bundles.Add(new StyleBundle("~/bundles/news.css")            .Include("~/common/screen_base.css",            "~/common/screen_news.css"));            bundles.Add(new ScriptBundle("~/bundles/news.js")            .Include("~/script/news_common.js",             "~/script/detail.js",             "~/script/share.js"));    }}

3. 在Global.asax的Application_Start中添加如下代碼:

BundleConfig.RegisterBundles(System.Web.Optimization.BundleTable.Bundles);

4. 在.aspx中添加如下的js/css引用代碼:

<head>    <title>IT新聞 - 部落格園</title>    <%: System.Web.Optimization.Styles.Render("~/bundles/news.css") %>    <%: System.Web.Optimization.Scripts.Render("~/bundles/news.js") %></head>

5. 運行結果:

<link href="/bundles/news.css?v=sWIyZt7YgHul_fjj5uctLQ6NK499bBwg7ev8KRgojN01" rel="stylesheet"/>
<script src="/bundles/news.js?v=h7kkNXn83HAKCJJjw2VNEzbW_hHG-e5sAPoNOs_2Cx41"></script>

System.Web.Optimization與SquishIt的主要不同之處:

SquishIt會產生實際的檔案儲存在對應的路徑。

System.Web.Optimization只在記憶體中產生bundle檔案。

更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/webkf/aspx/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.