Get the App
SLTechnology News&Howtos  ›  Development  › 

The role of ASP.NET virtual file system

Shulou Source: shulou.com Published: 2022-06-03 11:41:53 09月19日 Update

This article mainly explains "the function of ASP.NET virtual file system". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the role of ASP.NET virtual file system".

The specific implementation steps are as follows:

First, develop web user control

This step is no different from previous development.

1. First create a new web application (VS2005 sp1 support is required)

2. Then develop some web user controls in it

3. Right-click on the ascx file-> Properties-> generate operation to select embedded resources

4. Just generate dll (the name of dll is Test.Control.dll, which will be used later)

Second, develop a virtual file system to provide classes

This step is the most important step.

The specific idea is: register this class in the system, and then automatically call this class every time you access a file / resource, in this class to determine whether the path of the file is defined by us, if it is to use our logic to deal with it, that is, to take resources out of dll.

First of all, post the code of the class. I think maybe many people, like me, like to look at the code first:

DllVirtualPathProvider

Public class DllVirtualPathProvider: System.Web.Hosting.VirtualPathProvider

{

Public DllVirtualPathProvider ()

{

}

Public override string CombineVirtualPaths (string basePath, string relativePath)

{

If (IsAppResourcePath (basePath))

{

Return null

}

Return Previous.CombineVirtualPaths (basePath, relativePath)

}

Public override System.Runtime.Remoting.ObjRef CreateObjRef (Type requestedType)

{

Return Previous.CreateObjRef (requestedType)

}

Public override bool DirectoryExists (string virtualDir)

{

If (IsAppResourcePath (virtualDir))

{

Return true

}

Else

{

Return Previous.DirectoryExists (virtualDir)

}

}

Public override string GetCacheKey (string virtualPath)

{

If (IsAppResourcePath (virtualPath))

{

Return null

}

Else

{

Return Previous.GetCacheKey (virtualPath)

}

}

Public override string GetFileHash (string virtualPath

IEnumerable virtualPathDependencies)

{

If (IsAppResourcePath (virtualPath))

{

Return null

}

Else

{

Return Previous.GetFileHash (virtualPath, virtualPathDependencies)

}

}

Private bool IsAppResourcePath (string virtualPath)

{

String checkPath = VirtualPathUtility.ToAppRelative (virtualPath)

Return checkPath.StartsWith ("~ / MyUserControl/Test.Control.dll/"

StringComparison.InvariantCultureIgnoreCase)

}

Public override bool FileExists (string virtualPath)

{

Return (IsAppResourcePath (virtualPath) | | Previous.FileExists (virtualPath))

}

Public override VirtualFile GetFile (string virtualPath)

{

If (IsAppResourcePath (virtualPath))

{

Return new AssemblyResourceVirtualFile (virtualPath)

}

Else

{

Return Previous.GetFile (virtualPath)

}

}

Public override System.Web.Caching.CacheDependency GetCacheDependency (string virtualPath

System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart)

{

If (IsAppResourcePath (virtualPath))

{

String path = HttpRuntime.AppDomainAppPath + virtualPath.Substring (1)

Return new System.Web.Caching.CacheDependency (path)

}

Else

{

Return Previous.GetCacheDependency (virtualPath, virtualPathDependencies, utcStart)

}

}

}

Thank you for your reading, the above is the content of "the role of ASP.NET virtual file system", after the study of this article, I believe you have a deeper understanding of the role of ASP.NET virtual file system, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Files systems functions development resources learning code content that is ideas controls users builds important names properties applications situations articles times Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi NVidia Shulou Information MariaDB Apple