我正在使用VirtualPathProvider从数据库表中返回虚拟页面,但它一切正常,但问题是该站点不再识别页面物理存在的时间而不是保存在虚拟页面表中。
我正在使用的代码是在页面加载下面调试物理页面时函数FileExists返回false,页面加载失败并且“无法找到资源”。
如何解决这个问题的任何提示都会很棒!感谢
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
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("connstr").ConnectionString
Dim cnn As New SqlConnection(strConn)
cnn.Open()
Dim cmd As New SqlCommand()
cmd.Connection = cnn
cmd.CommandText = "select count(*) from webforms where virtualpath='" & virtualPath & "'"
Dim retval As Object = cmd.ExecuteScalar()
cnn.Close()
Dim i As Integer = Convert.ToInt32(retval)
If i <= 0 Then
Return False
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
答案 0 :(得分:2)
查看VirtualPathProvider.Previous
财产。 MSDN说,Previous属性获取对编译系统中先前注册的VirtualPathProvider对象的引用。
所以你应该修改一下你的FileExists
方法,如:
Dim i As Integer = Convert.ToInt32(retval)
If i <= 0 Then
Return Previous.FileExists(virtualPath)
Else
Return True
End If
不应修改 GetFile
方法,因为您已在那里使用Previous.GetFile(virtualPath)
。