我使用VirtualPathProvider从SQL Server表返回虚拟页面。这是工作pk,我的VirtualPathProvider类文件中的代码如下所示。
我遇到的问题是,当我更改保存在数据库中的虚拟页面数据(标题或页面文本)时,此更改不会显示在输出页面上,因为原始页面已被缓存。
我已经阅读了一些关于向VirtualPathProvider类添加GetCacheDependancy的文章,并尝试实现了几个示例,但是原始页面仍然被缓存并显示。
我还尝试在虚拟页面(Response.AddCacheItemDependency("Pages"))的页面加载中添加一些代码,并编辑global.asax:
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application startup
HttpContext.Current.Cache.Insert("Pages", DateTime.Now, Nothing, _
System.DateTime.MaxValue, System.TimeSpan.Zero, _
System.Web.Caching.CacheItemPriority.NotRemovable, _
Nothing)以防止缓存。但是什么都不起作用。
因此,我要做的是对VirtualPathProvider类文件进行一些更改,以防止这些缓存问题。感谢您能提供的任何帮助!
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.Hosting
Imports System.Web.UI.MobileControls
Imports System.Collections.Generic
Public Class DbVirtualPathProvider
Inherits VirtualPathProvider
Public Shared Sub AppInitialize()
Dim db As New DbVirtualPathProvider()
HostingEnvironment.RegisterVirtualPathProvider(db)
End Sub
Public Overrides Function FileExists(ByVal virtualPath As String) As Boolean
Dim strConn As String = ConfigurationManager.ConnectionStrings("LIQUIDConnectionString").ConnectionString
Dim cnn As New SqlConnection(strConn)
cnn.Open()
Dim cmd As New SqlCommand()
cmd.Connection = cnn
cmd.CommandText = "select count(*) from tbl_VirtualFiles where virtualpath='" & virtualPath & "'"
Dim retval As Object = cmd.ExecuteScalar()
cnn.Close()
Dim i As Integer = Convert.ToInt32(retval)
If i <= 0 Then
'important as if no virtual file it looks for physical file
Return Previous.FileExists(virtualPath)
Else
Return True
End If
End Function
Public Overrides Function GetFile(ByVal virtualPath As String) As VirtualFile
Dim file As New DbVirtualFile(virtualPath)
If file.WebFormContent Is Nothing Then
Return Previous.GetFile(virtualPath)
Else
Return file
End If
End Function
End Class发布于 2011-04-08 20:49:47
我认为您需要使用与FileExists和GetFile方法相同的方式覆盖虚拟路径提供程序的VirtualPathProvider.GetCacheDependency方法。所以可以这样做,例如:
Public Overrides Function GetCacheDependency(virtualPath As String, virtualPathDependencies As IEnumerable, utcStart As DateTime) As CacheDependency
If IsVirtualPathForDatabase(virtualPath) Then
Return New CacheDependency(...)
Else
Return New Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart)
End If
End Functionwhere is fake IsVirtualPathForDatabase方法应该确定虚拟路径是否与您的数据库相关。如果是,您可以为此创建自己的CacheDependency (我为它留出了位置...),否则您可以对所有物理.aspx页面使用Previous.GetCacheDependency。
https://stackoverflow.com/questions/5593421
复制相似问题