Connecting Tech Pros Worldwide Forums | Help | Site Map

Help with my Virtual Path Provider Class

dba123
Guest
 
Posts: n/a
#1: Jul 18 '06
I'm trying to implement my own Virtual Path Provider class.

http://msdn2.microsoft.com/en-us/lib...hprovider.aspx

http://support.microsoft.com/?scid=k...d=8940&sid=513

http://msdn.microsoft.com/library/de...ml/vpp_vga.asp

However, I'm not going to be using an external store such as a DB, etc.
because we are only going to be watching for 3 unique URLs. If those
incoming URLs are found , then I want to serve a specific page in a specific
location.

FYI, I'm testing this locally on my PC using the VS web server.

What we want to do is for 3 URLs that we define that are incoming from the
client when they click on a link off our website (I'll click on them from an
aspx page GridView to test this when this is done), trick the FileExists
method to say it exists. Then call the GetFile method to retrieve the
content of a file we specify..basically the same file but want to serve it
back from a specific location from our file system using VPP
(\search\SearchResults.aspx).

So far, here's a sort of sketch code I've created. I need all the help I
can get to understand how to finish coding this, I have not a clue if I'm
doing this right...I'm new to both VPP and C# and my head is spinning at this
point:

using System;
using System.IO;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public class My_VPP_Provider : System.Web.Hosting.VirtualPathProvider
{

public static void Appinitialize()
{
My_VPP_Provider fileProvider = new My_VPP_Provider();

System.Web.Hosting.HostingEnvironment.RegisterVirt ualPathProvider(fileProvider);
}

switch virtualPath
{
case "/recipes/poultry/SearchResults.aspx":
GetFile("/Search/SearchResults.aspx")
case "/recipes/meat/SearchResults.aspx":
GetFile("/Search/SearchResults.aspx")
case "/recipes/fish/SearchResults.aspx":
GetFile("/Search/SearchResults.aspx")
case "/recipes/seafood/SearchResults.aspx":
GetFile("/Search/SearchResults.aspx")
case "/recipes/vegetables/SearchResults.aspx":
GetFile("/Search/SearchResults.aspx")
default:
System.Web.Hosting.VirtualPathProvider
}


public override bool FileExists(string virtualPath)
{
string contents = virtualPath;
if (contents.Equals(string.Empty))
return false;
else
return true;
}

public override System.Web.Hosting.VirtualFile GetFile(string virtualPath)
{
string contents = virtualPath;
if (contents.Equals(string.Empty))
return Previous.GetFile(virtualPath);
else
return new MyVirtualFile(virtualPath, contents);
}
}

public class MyVirtualFile : System.Web.Hosting.VirtualFile
{
private string _FileContent;

public MyVirtualFile(string virtualPath, string fileContent)
: base(virtualPath)
{
_FileContent = fileContent;
}

public override Stream Open()
{
Stream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream, Encoding.Unicode);

writer.Write(_FileContent);
writer.Flush();
stream.Seek(0, SeekOrigin.Begin);
return stream;
}
}


--
dba123

Closed Thread