asp.net VirtualPathProvider - 不再识别物理文件

发布于 2024-10-30 15:06:33 字数 1692 浏览 4 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安静 2024-11-06 15:06:34

查看 VirtualPathProvider.Previous< /code>属性。 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) 那里。

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:

Dim i As Integer = Convert.ToInt32(retval)
If i <= 0 Then
    Return Previous.FileExists(virtualPath)
Else
    Return True
End If

GetFile method shouldn't be modified because you're already use Previous.GetFile(virtualPath) there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文