如何获取 VB.NET 中 Bundle 的 "context" 值?
How do I get the "context" value of a Bundle in VB.NET?
我正在尝试迭代已添加到特定包的文件列表。
例如,如果我有一个这样创建的包:
'_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
根据 MSDN docs,BundleContext
是一个 class,其构造函数接受 3 个参数,如下所示:
Public Class BundleContext
Public Sub New (context As HttpContextBase, collection As BundleCollection, bundleVirtualPath As String)
End Class
因此,您需要传递 HttpContext
、BundleTable.Bundles
(这是默认值 BundleCollection
,如果有,请更改为自定义包集合)和包虚拟路径以创建 BundleContext
像这样的例子:
Dim currentContext As HttpContext = HttpContext.Current
Dim contextWrapper As HttpContextWrapper
contextWrapper = New HttpContextWrapper(currentContext)
Dim myContext As BundleContext
myContext = New BundleContext(contextWrapper, BundleTable.Bundles, "~/bundles/example")
我正在尝试迭代已添加到特定包的文件列表。
例如,如果我有一个这样创建的包:
'_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
根据 MSDN docs,BundleContext
是一个 class,其构造函数接受 3 个参数,如下所示:
Public Class BundleContext
Public Sub New (context As HttpContextBase, collection As BundleCollection, bundleVirtualPath As String)
End Class
因此,您需要传递 HttpContext
、BundleTable.Bundles
(这是默认值 BundleCollection
,如果有,请更改为自定义包集合)和包虚拟路径以创建 BundleContext
像这样的例子:
Dim currentContext As HttpContext = HttpContext.Current
Dim contextWrapper As HttpContextWrapper
contextWrapper = New HttpContextWrapper(currentContext)
Dim myContext As BundleContext
myContext = New BundleContext(contextWrapper, BundleTable.Bundles, "~/bundles/example")