我正在使用VirtualPathProvider从SQL Server表返回虚拟页。这是有效的pk,我的VirtualPathProvider类文件中的代码如下。

我遇到的问题是,当我更改数据库中保存的虚拟页面数据(标题或页面文本)时,由于已缓存原始页面,因此在输出的页面上未显示此更改。

我已经阅读了几篇有关将GetCacheDependancy添加到VirtualPathProvider类的文章,并且我尝试实现一些示例,但是原始页面仍在缓存和显示中。

我还尝试将一些代码添加到虚拟页面(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


末级

最佳答案

我认为您需要以与VirtualPathProvider.GetCacheDependencyFileExists方法相同的方式为虚拟路径提供程序覆盖GetFile方法。因此可以做到通过这种方式:

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 Function


伪造的IsVirtualPathForDatabase方法应在何处确定虚拟路径是否与您的数据库相关。如果是,则可以为此创建自己的CacheDependency(我为它保留一个地方...),否则,可以对所有物理Previous.GetCacheDependency页面使用.aspx

10-05 18:12