ASP. NET virtualpathprovider (I)

Source: Internet
Author: User
In some special circumstances, the conventional file and directory Organization Methods of websites cannot meet the requirements. In Asp.net 2.0, some classes are provided to implement their own virtual file systems, you can save website files in ZIP files, binary files in custom formats, and even databases. These classes are defined in the system. Web. Hosting namespace, such as virtualpathprovider, virtualfile, and virtualdirectory.

Previously, the implementation of page or file hiding can inherit ihttphandler and ihttpmodule, and then be configured in the web. comfig file. In comparison, virtualpathprovider is more powerful.

Files or folders that can be mapped to a virtual file system include:

ASP. NET page, master page, user control, and other objects.
A standard webpage with an extension such as. htm and. jpg.
Any custom extension mapped to the buildprovider instance.
Any named topic in the app_theme folder.

However, the following folders or files cannot be stored in the Virtual File System:

Global. asax file.
Web. config file.
The site map data file used by xmlsitemapprovider.
Include or generate an application Program Assembly Directory: Bin, app_code, app_globalresources, and any app_localresources directory.
App_data in the application data folder.

1. Customize your own virtual path provider
Inherits the virtualpathprovider class.UsingSystem. IO;
UsingSystem. Web;
UsingSystem. Web. Hosting;
UsingSystem. text;

Namespace Xianfen. net. myvirtualpathprovider_file
{
Public Class Myvirtualpathprovider: virtualpathprovider
{
Public Override Bool Fileexists ( String Virtualpath)
{
Virtualpath
= Virtualpath. tolower ();

If (Virtualpath. endswith ( " Myvirtualfile. aspx " ))
{
Return True ;
}
Else
{
Return Previous. fileexists (virtualpath );
}
}

Public OverrideVirtualfile GetFile (StringVirtualpath)
{
Virtualpath
=Virtualpath. tolower ();

If (Virtualpath. endswith ( " Myvirtualfile. aspx " ))
{
Return New Myvirtualfile (virtualpath );
}
Else
{
Return Previous. GetFile (virtualpath );
}
}
}
}

2. Customize your own virtual files
Inherits the virtualfile class.NamespaceXianfen. net. myvirtualpathprovider_file
{
Public ClassMyvirtualfile: virtualfile
{
Private StringMypath;

PublicMyvirtualfile (StringVirtualpath)
:
Base(Virtualpath)
{
Mypath
=Virtualpath;
}

Public Override Stream open ()
{
Stream stream
= New Memorystream ();
Streamwriter SW
= New Streamwriter (Stream );
Streamreader SR
= Null ;

Try
{
If (Mypath. endswith ( " Myvirtualfile. aspx " ))
{
Sr
= New Streamreader (
Httpcontext. Current. server. mappath (
" ~ /App_data/htm.txt " ), Encoding. utf8 );
}

Sw. Write (Sr. readtoend ());
Sw. Flush ();
Stream. Seek (0, Seekorigin. Begin );

Return Stream;
}
Finally
{
If (SR ! = Null )
{< br> Sr. close ();
Sr. dispose ();
}< BR >}

3. register the virtual path provider.
there are three registration methods.
(1) Register appinitialize in this special method: Public static void appinitialize ()
{< br> hostingenvironment.
registervirtualpathprovider (
New myvirtualpathprovider ();
}

(2) register in the application_start method of Global. asax. protected void application_start ( Object sender, eventargs e)
{< br> hostingenvironment.
registervirtualpathprovider (
New myvirtualpathprovider ();
}

(3) inherit the ihttpmodule and register it in the event handler of httpapplication. beginrequest. Public class myihttpmodule: ihttpmodule
{< br>
Public void dispose ()
{< BR >}

Public void Init (httpapplication context)
{< br> context. beginrequest
+= New eventhandler (context_beginrequest );
}

void context_beginrequest ( Object sender, eventargs e)
{< br> hostingenvironment.
registervirtualpathprovider (
New myvirtualpathprovider ();
}< BR >}

Configuration in Web. config:<Httpmodules>
<AddName= "Mymodule"
Type
= "Xianfen. net. myvirtualpathprovider_file.myihttpmodule, xianfen. net. myvirtualpathprovider_file"/>
</Httpmodules>

Run the command to find the Virtual File.

References: http://msdn.microsoft.com/zh-cn/library/system.web.hosting.virtualpathprovider.aspx

sample download : Click to download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.