在ASP.Net中可使用打包與壓縮這兩種技術來提升Web應用程序頁面加載的性能。經過減小從服務器請求的次數和減小資源文件的體積來提升加載性能。css
例以下面JavaScript函數:html
AddAltToImg = function (imageTagAndImageID, imageContext) { ///<signature> ///<summary> Adds an alt tab to the image // </summary> //<param name="imgElement" type="String">The image selector.</param> //<param name="ContextForImage" type="String">The image context.</param> ///</signature> var imageElement = $(imageTagAndImageID, imageContext); imageElement.attr('alt', imageElement.attr('id').replace(/ID/, '')); }
壓縮後,函數簡化爲以下:git
AddAltToImg=function(t,a){var r=$(t,a);r.attr("alt",r.attr("id").replace(/ID/,""))};
除了刪除註釋和沒必要要的空格以外,參數和變量名稱被重命名(縮寫)以下:github
本文將介紹使用的打包和壓縮的優勢,以及如何在ASP.NET Core應用程序中使用這些功能。json 概述在ASP.Net中可使用打包與壓縮這兩種技術來提升Web應用程序頁面加載的性能。經過減小從服務器請求的次數和減小資源文件的體積來提升加載性能。數組
例以下面JavaScript函數:服務器 AddAltToImg = function (imageTagAndImageID, imageContext) { ///<signature> ///<summary> Adds an alt tab to the image // </summary> //<param name="imgElement" type="String">The image selector.</param> //<param name="ContextForImage" type="String">The image context.</param> ///</signature> var imageElement = $(imageTagAndImageID, imageContext); imageElement.attr('alt', imageElement.attr('id').replace(/ID/, '')); } 壓縮後,函數簡化爲以下:app AddAltToImg=function(t,a){var r=$(t,a);r.attr("alt",r.attr("id").replace(/ID/,""))}; 除了刪除註釋和沒必要要的空格以外,參數和變量名稱被重命名(縮寫)以下:ide
此示例來自於github:https://github.com/aspnet/Docs/blob/master/aspnetcore/client-side/bundling-and-minification.md bundleconfig.json文件MVC項目模板提供了一個
配置選項詳細說明:
在項目中使用打包和壓縮在VS 2015/2017須要安裝BundlerMinifierVsix,安裝完成後須要重啓VS。 在 [ { "outputFileName": "wwwroot/css/site.min.css", "inputFiles": [ "wwwroot/css/site.css" ] }, { "outputFileName": "wwwroot/js/site.min.js", "inputFiles": [ "wwwroot/js/site.js" ], "minify": { "enabled": true, "renameLocals": true }, "sourceMap": false } ]
在
在項目中會分別生成壓縮後的資源文件。
在視圖中使用打包壓縮後的資源文件在上一篇博客《ASP.NET Core配置環境變量和啓動設置》咱們已經討論過環境變量,在視圖中經過 Environment 標籤,分別定義開發、預演和生產環境加載對應的資源文件。 <environment names="Development"> <link rel="stylesheet" href="~/css/site.css" /> <script src="~/js/site.js" asp-append-version="true"></script> </environment> <environment names="Staging,Production"> <link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" /> <script src="~/js/site.min.js" asp-append-version="true"></script> </environment> 當在開發模式下運行應用程序,咱們使用未壓縮Css和腳本文件;在生產環境中,咱們壓縮後的資源文件,這樣能夠提升應用程序的性能。 總結在ASP.Net中可使用打包與壓縮這兩種技術來提升Web應用程序頁面加載的性能。但願上面的內容對你們的學習有所幫助,謝謝! |
重命名後 | ||||||||
---|---|---|---|---|---|---|---|---|---|
imageTagAndImageID | t | ||||||||
imageContext | a | ||||||||
imageElement | r |