我在https://devblog.appriver.com/easy-cssjs-bundles-for-webforms-mvc-apps/ 找到了代码,我将其用作以下解决方案的起点。
此解决方案将允许您将单个文件添加到新包中,就像在上面给出的链接中找到的类一样。
但是,此解决方案还允许您指定要添加到此新捆绑包中的其他捆绑包。当给定包路径时,此类将获取包中定义的文件路径,并将这些文件包含到创建的新包中。
从其他现有样式包创建新样式包时,此类将检测指定了 CssRewriteUrlTransform 类的文件路径,并将该转换应用于新创建的包中的同一文件。
用法:
我在定义包含我们创建的所有各种捆绑包的 BundleConfig 类的同一个文件的末尾添加了这个类:
Public Class BundleManager
#Region "Script Bundling"
Public Overloads Shared Function ScriptBundle(newBundlePath As String, ParamArray bundleOrFilePaths As String()) As IHtmlString
' used when looping dictionaries below
Dim pair As KeyValuePair(Of String, Boolean)
' Loops through all bundleOrFilePaths given and:
' --If the path is for a bundle then adds the filepaths in that bundle to the allFilePaths list
' --If the path is for a single file then adds that file path to the allFilePaths list
' The end result is a list of individual files to include in the new on-the-fly bundle
Dim allFilePaths As New Dictionary(Of String, Boolean)
For idx As Integer = 0 To bundleOrFilePaths.Length - 1
Dim filepaths As Dictionary(Of String, Boolean) = GetFilepathsInBundle(bundleOrFilePaths(idx), False)
For Each pair In filepaths
If allFilePaths.ContainsKey(pair.Key) = False Then
allFilePaths.Add(pair.Key, pair.Value)
End If
Next
Next
Dim thisBundle = New ScriptBundle(newBundlePath)
For Each pair In allFilePaths
thisBundle.Include(pair.Key)
Next
BundleTable.Bundles.Add(thisBundle)
Return Scripts.Render(newBundlePath)
End Function
#End Region
#Region "Style Bundling"
Public Overloads Shared Function StyleBundle(newBundlePath As String, ParamArray bundleOrFilePaths As String()) As IHtmlString
' used when looping dictionaries below
Dim pair As KeyValuePair(Of String, Boolean)
' Loops through all bundleOrFilePaths given and:
' --If the path is for a bundle then adds the filepaths in that bundle to the allFilePaths list
' --If the path is for a single file then adds that file path to the allFilePaths list
' The end result is a list of individual files to include in the new on-the-fly bundle
Dim allFilePaths As New Dictionary(Of String, Boolean)
For idx As Integer = 0 To bundleOrFilePaths.Length - 1
Dim filepaths As Dictionary(Of String, Boolean) = GetFilepathsInBundle(bundleOrFilePaths(idx), True)
For Each pair In filepaths
If allFilePaths.ContainsKey(pair.Key) = False Then
allFilePaths.Add(pair.Key, pair.Value)
End If
Next
Next
Dim thisBundle = New StyleBundle(newBundlePath)
For Each pair In allFilePaths
If pair.Value = True Then
thisBundle.Include(pair.Key, New CssRewriteUrlTransform())
Else
thisBundle.Include(pair.Key)
End If
Next
BundleTable.Bundles.Add(thisBundle)
Return Styles.Render(newBundlePath)
End Function
#End Region
Public Shared Function GetFilepathsInBundle(ByVal bundleOrFilePath As String, Optional ByVal lookForCssRewriteUrlTransform As Boolean = False) As Dictionary(Of String, Boolean)
Dim filepaths As New Dictionary(Of String, Boolean)
Dim myBundle As System.Web.Optimization.Bundle
myBundle = BundleTable.Bundles.GetBundleFor(bundleOrFilePath)
If myBundle Is Nothing Then
' This isn't a bundle path so it's just a file path, add it to the filepaths list and return
filepaths.Add(bundleOrFilePath, False)
Return filepaths
End If
Dim currentContext As HttpContext = HttpContext.Current
Dim httpCtxt As HttpContextWrapper
httpCtxt = New HttpContextWrapper(currentContext)
Dim myContext As BundleContext
myContext = New BundleContext(httpCtxt, BundleTable.Bundles, bundleOrFilePath)
Dim myIEnumerable As IEnumerable(Of BundleFile)
myIEnumerable = myBundle.EnumerateFiles(myContext)
For Each bundlefile In myIEnumerable.ToArray
Dim useCssRewriteUrlTransform As Boolean = False
If lookForCssRewriteUrlTransform = True AndAlso bundlefile.Transforms.Count > 0 Then
useCssRewriteUrlTransform = True
End If
filepaths.Add(bundlefile.IncludedVirtualPath, useCssRewriteUrlTransform)
Next
Return filepaths
End Function
End Class
** 在您的剃须刀页面中 **
为浏览器创建单个样式和单个脚本包以供下载
通过组合此特定剃须刀页面使用的所有捆绑包和单个文件。
注意,StyleBundle/ScriptBundle 的第一个参数是 NEW 捆绑包的路径,该捆绑包将被即时创建和使用。
将其放置在您想要在页面上包含样式的位置:
@BundleManager.StyleBundle("~/styles/onTheFly",
"~/existing/style/bundle1",
"~/existing/style/bundle2",
"~/path/to/individual/file.css",
"~/existing/style/bundle3",
"~/path/to/another/individual/file.css"
)
' will result in something like:
' <link href="/styles/onthefly?v=tNdHeAW5fTG_oge_eQoH0xgkG9tvARsxlz864XQUBBk1" rel="stylesheet" />
将其放置在页面上要包含脚本的位置:
@BundleManager.ScriptBundle("~/scripts/onTheFly",
"~/existing/script/bundle1",
"~/existing/script/bundle2",
"~/path/to/individual/file.js",
"~/existing/script/bundle3",
"~/path/to/another/individual/file.js"
)
' Will result in something like:
' <link href="/scripts/onthefly?v=tNdHeAW5fTG_oge_eQoH0xgkG9tvARsxlz864XQUBBk1" rel="stylesheet" />