asp.net VirtualPathProvider - 不再识别物理文件
我使用 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
I'm using VirtualPathProvider to return virtual pages from a database table and it's all working fine but the problem is that the site no longer recognises when a page exists physically rather than being held in the virtual pages table.
The code I'm using is below on page load when debugging a physical page when the function FileExists Returns false the page load fails and 'resource cannot be found'.
any tips of how to solve this would be great! thanks
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看
VirtualPathProvider.Previous< /code>
属性。 MSDN 说 Previous 属性获取对编译系统中先前注册的 VirtualPathProvider 对象的引用。
因此,您应该稍微修改一下
FileExists
方法,例如:GetFile
方法不应修改,因为您已经使用了Previous.GetFile(virtualPath)
那里。Take a look onto
VirtualPathProvider.Previous
property. MSDN says that Previous property gets a reference to a previously registered VirtualPathProvider object in the compilation system.So you should modify a little your
FileExists
method like:GetFile
method shouldn't be modified because you're already usePrevious.GetFile(virtualPath)
there.