I'm using VirtualPathProvider
to return virtual pages from a SQL Server table. This is working pk and the code in my VirtualPathProvider
class file is below.
The problem I'm having is that when I make a change the virtual page data (title or page text) held in the database, this change isn't shown on the page that's output as the original page has been cached.
I've read a few articles about adding GetCacheDependancy to the VirtualPathProvider class and I've tried implementing a couple of examples but the original page is still cached and displaying.
I've also tried adding some code to the page load of the virtual pages (Response.AddCacheItemDependency("Pages")
) and editing the 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)
to prevent the caching. but nothings working.
So what I'm after is some changes to by VirtualPathProvider class file to prevent these caching problems. Thanks for any help you can offer!
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