开发者

asp.net VirtualPathProvider - no longer recognising physical files

开发者 https://www.devze.com 2023-02-22 13:47 出处:网络
I\'m using VirtualPathProvider to return virtual pages from a database table and it\'s all working fine but the problem is that the si开发者_高级运维te no longer recognises when a page exists physical

I'm using VirtualPathProvider to return virtual pages from a database table and it's all working fine but the problem is that the si开发者_高级运维te 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消