Help...wimper.. been trying to get a virtualpathprovider to work and I have
to be missing something. I want users to be able to enter something like
mydomain.com/userentereddirectory/ and have that serve up a file. So I use
vpp to code up an initialization even, virturalfile, virtualdirectory and
virtualpath provider. I have a simple file for testing testing.aspx (no
codebehind) that just sets a label to 'hello world' on pageload. Now when I
debug and just to go mydomain.com/userentereddirectory - I get the text that
makes up my tester.aspx file return but its not compiled or executed - if i
specify mydomaing.com/user../tester.aspx I get 'Directory 'C:\Documents and
Settings\Ken\My Documents\Visual Studio 2005\WebSites\HuningtonManor\926'
does not exist. Failed to start monitoring file changes.' Can't figure out
what I'm doing wrong - I looked at all the tutorials out there and everything
seems to be working as it should?! Code to follow.. All code is in app_code
- now I merely hardcoded 926 as the begining to test for a virtual directory
- just tryng to make this work before i put in my true implementation.
<b><i>RegisterProvider</i></b>
Namespace HuntingtonManor.Framework
Public Class AppStart
Public Shared Sub AppInitialize()
Dim DynamicProvider As DynamicPathProvider = New
DynamicPathProvider()
HostingEnvironment.RegisterVirtualPathProvider(Dyn amicProvider)
End Sub
End Class
End Namespace
<b><i>VirtualPathProvider</i></b>
Namespace HuntingtonManor.Framework
<AspNetHostingPermission(SecurityAction.Demand,
Level:=AspNetHostingPermissionLevel.Medium), _
AspNetHostingPermission(SecurityAction.Inheritance Demand,
level:=AspNetHostingPermissionLevel.High)> _
Public Class DynamicPathProvider
Inherits VirtualPathProvider
Public Sub New()
MyBase.New()
End Sub
Protected Overrides Sub Initialize()
End Sub
Private Function IsPathVirtual(ByVal virtualPath As String) As Boolean
'Return True
Dim checkPath As String
checkPath = VirtualPathUtility.ToAppRelative(virtualPath)
Return checkPath.StartsWith("~/926",
StringComparison.InvariantCultureIgnoreCase)
End Function
Public Overrides Function FileExists(ByVal virtualPath As String) As
Boolean
If (IsPathVirtual(virtualPath)) Then
Dim file As DynamicVirtualFile
file = CType(GetFile(virtualPath), DynamicVirtualFile)
'Return file.Exists
Return True
Else
Return Previous.FileExists(virtualPath)
End If
End Function
Public Overrides Function DirectoryExists(ByVal virtualDir As
String) As Boolean
If (IsPathVirtual(virtualDir)) Then
Dim dir As DynamicVirtualDirectory
dir = CType(GetDirectory(virtualDir), DynamicVirtualDirectory)
'Return dir.exists
Return True
Else
Return Previous.DirectoryExists(virtualDir)
End If
End Function
Public Overrides Function GetDirectory(ByVal virtualDir As String)
As System.Web.Hosting.VirtualDirectory
If (IsPathVirtual(virtualDir)) Then
Return New DynamicVirtualDirectory(virtualDir, Me)
Else
Return Previous.GetDirectory(virtualDir)
End If
End Function
Public Overrides Function GetFile(ByVal virtualPath As String) As
System.Web.Hosting.VirtualFile
If (IsPathVirtual(virtualPath)) Then
Return New DynamicVirtualFile(virtualPath, Me)
Else
Return Previous.GetFile(virtualPath)
End If
End Function
End Class
End Namespace
<b><i>VirtualFileProvider</b></i>
Namespace HuntingtonManor.Framework
<AspNetHostingPermission(SecurityAction.Demand,
Level:=AspNetHostingPermissionLevel.Minimal), _
AspNetHostingPermission(SecurityAction.Inheritance Demand,
level:=AspNetHostingPermissionLevel.Minimal)> _
Public Class DynamicVirtualFile
Inherits VirtualFile
Private spp As DynamicPathProvider
Public Sub New(ByVal virtualPath As String, ByVal provider As
DynamicPathProvider)
MyBase.New(virtualPath)
spp = provider
End Sub
Public Overrides Function Open() As System.IO.Stream
'this is the file I want to serve
Dim templateFile As String
templateFile = HostingEnvironment.ApplicationPhysicalPath &
"tester.aspx"
Dim pageTemplate As String
pageTemplate = My.Computer.FileSystem.ReadAllText(templateFile)
' Put the page content on the stream.
Dim stream As MemoryStream
stream = New MemoryStream()
Dim writer As StreamWriter
writer = New StreamWriter(stream)
writer.Write(pageTemplate)
writer.Flush()
stream.Seek(0, SeekOrigin.Begin)
Return stream
End Function
End Class
End Namespace
<b><i>VirtualDirectoryProvider</i></b>
Namespace HuntingtonManor.Framework
<AspNetHostingPermission(SecurityAction.Demand,
Level:=AspNetHostingPermissionLevel.Minimal), _
AspNetHostingPermission(SecurityAction.Inheritance Demand,
level:=AspNetHostingPermissionLevel.Minimal)> _
Public Class DynamicVirtualDirectory
Inherits VirtualDirectory
Private spp As DynamicPathProvider
' Declare the variable the property uses.
Private existsValue As Boolean
Public ReadOnly Property exists() As Boolean
Get
Return existsValue
End Get
End Property
Public Sub New(ByVal virtualDir As String, ByVal provider As
DynamicPathProvider)
MyBase.New(virtualDir)
spp = provider
End Sub
Private childrenValue As ArrayList
Public Overrides ReadOnly Property Children() As
System.Collections.IEnumerable
Get
Return childrenValue
End Get
End Property
Private directoriesValue As ArrayList
Public Overrides ReadOnly Property Directories() As
System.Collections.IEnumerable
Get
Return directoriesValue
End Get
End Property
Private filesValue As ArrayList
Public Overrides ReadOnly Property Files() As
System.Collections.IEnumerable
Get
Return filesValue
End Get
End Property
End Class
End Namespace
<b><i>Tester.aspx</i></b>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
Me.Label1.Text = "blah blah blah"
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
</form>
</body>
</html>