我使用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
看一下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)
。