我正在尝试迭代已添加到特定包中的文件列表。
例如,如果我有一个像这样创建的包:
'_App_Start.vbhtml:
BundleConfig.RegisterBundles(System.Web.Optimization.BundleTable.Bundles)
'BundleConfig.vb:
Imports System.Web.Optimization
Public Class BundleConfig
Public Shared Sub RegisterBundles(ByRef bundles As BundleCollection)
bundles.Add(New ScriptBundle("~/bundles/example").Include(
"~/Scripts/file1.js",
"~/Scripts/file2.js"
))
End Sub
End Class
从项目中的其他* .vbhtml文件中,我希望能够迭代一个包,"〜/ bundles / example"在这个例子中,为了获得文件列表,"〜/ Scripts / file1.js"和"〜/ Scripts / file2.js",在这个例子中。
这是我到目前为止所拥有的:
Dim myBundle As System.Web.Optimization.Bundle
myBundle = BundleTable.Bundles.GetBundleFor("~/bundles/example")
'== Stuck Here:
Dim myContext As BundleContext
myContext = "** Pleae Help **"
Dim myIEnumerable As IEnumerable(Of BundleFile)
myIEnumerable = myBundle.EnumerateFiles(myContext)
查看Bundle.EnumerateFiles方法(https://msdn.microsoft.com/en-us/library/system.web.optimization.bundle.enumeratefiles(v=vs.110).aspx)的文档,我看到第一个参数是" BundleContext"。
这是我 卡住的地方! 我如何或在何处将BundleContext值传递给EnumerateFiles方法?
修改 - 使用Tetsuya的答案提供的完整工作示例
Dim myBundle As System.Web.Optimization.Bundle
myBundle = BundleTable.Bundles.GetBundleFor("~/bundles/example")
Dim currentContext As HttpContext = httpContext.Current
Dim httpCtxt As HttpContextWrapper
httpCtxt = New HttpContextWrapper(currentContext)
Dim myContext As BundleContext
myContext = New BundleContext(httpCtxt, BundleTable.Bundles, "~/bundles/example")
Dim myIEnumerable As IEnumerable(Of BundleFile)
myIEnumerable = myBundle.EnumerateFiles(myContext)
Dim filepath As String
For Each bundlefile In myIEnumerable.ToArray
filepath = bundlefile.IncludedVirtualPath
' do stuff ..
Next
答案 0 :(得分:1)
根据MSDN docs,BundleContext
是一个类,其构造函数接受3个参数,如下所示:
Public Class BundleContext
Public Sub New (context As HttpContextBase, collection As BundleCollection, bundleVirtualPath As String)
End Class
因此,您需要传递HttpContext
,BundleTable.Bundles
(这是默认的BundleCollection
,如果您有任何更改,请更改为自定义捆绑集合)并捆绑虚拟路径以创建{{1像这个例子的实例:
BundleContext