Click or drag to resize

EmbeddedResourcePathProvider Class

A VirtualPathProvider that allows serving pages from embedded resources.
Inheritance Hierarchy
SystemObject
  SystemMarshalByRefObject
    System.Web.HostingVirtualPathProvider
      GSF.Web.HostingEmbeddedResourcePathProvider

Namespace: GSF.Web.Hosting
Assembly: GSF.Web (in GSF.Web.dll) Version: 2.4.218-beta
Syntax
public class EmbeddedResourcePathProvider : VirtualPathProvider
View Source

The EmbeddedResourcePathProvider type exposes the following members.

Constructors
 NameDescription
Public methodEmbeddedResourcePathProviderInitializes a new instance of the EmbeddedResourcePathProvider class
Top
Properties
 NameDescription
Public propertyAllowOverrides Gets a value indicating if embedded files can be overridden.
Public propertyFiles Gets the collection of files served by this provider.
Protected propertyPreviousGets a reference to a previously registered VirtualPathProvider object in the compilation system.
(Inherited from VirtualPathProvider)
Top
Methods
 NameDescription
Public methodCombineVirtualPathsCombines a base path with a relative path to return a complete path to a virtual resource.
(Inherited from VirtualPathProvider)
Public methodCreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
(Inherited from MarshalByRefObject)
Public methodDirectoryExistsGets a value that indicates whether a directory exists in the virtual file system.
(Inherited from VirtualPathProvider)
Public methodEqualsDetermines whether the specified object is equal to the current object.
(Inherited from Object)
Public methodFileExists Gets a value that indicates whether a file exists in the virtual file system.
(Overrides VirtualPathProviderFileExists(String))
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object)
Public methodGetCacheDependency Creates a cache dependency based on the specified virtual paths.
(Overrides VirtualPathProviderGetCacheDependency(String, IEnumerable, DateTime))
Public methodGetCacheKeyReturns a cache key to use for the specified virtual path.
(Inherited from VirtualPathProvider)
Protected methodStatic memberGetConfiguredAssemblyNames Gets the names of the configured assemblies from configuration.
Public methodGetDirectoryGets a virtual directory from the virtual file system.
(Inherited from VirtualPathProvider)
Public methodGetFile Gets a virtual file from the virtual file system.
(Overrides VirtualPathProviderGetFile(String))
Public methodGetFileHashReturns a hash of the specified virtual paths.
(Inherited from VirtualPathProvider)
Public methodGetHashCodeServes as the default hash function.
(Inherited from Object)
Public methodGetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance.
(Inherited from MarshalByRefObject)
Public methodGetTypeGets the Type of the current instance.
(Inherited from Object)
Protected methodInitialize Initializes the VirtualPathProvider instance.
(Overrides VirtualPathProviderInitialize)
Public methodInitializeLifetimeServiceGives the VirtualPathProvider object an infinite lifetime by preventing a lease from being created.
(Inherited from VirtualPathProvider)
Protected methodStatic memberCode exampleMapResourceToWebApplication Maps an embedded resource ID into a web application relative path (~/path).
Protected methodMemberwiseCloneCreates a shallow copy of the current Object.
(Inherited from Object)
Protected methodMemberwiseClone(Boolean)Creates a shallow copy of the current MarshalByRefObject object.
(Inherited from MarshalByRefObject)
Protected methodProcessEmbeddedFiles Reads in the embedded files from an assembly an processes them into the virtual file system.
Public methodToStringReturns a string that represents the current object.
(Inherited from Object)
Top
Fields
 NameDescription
Public fieldStatic memberConfigKeyAllowOverrides Appsettings key that indicates if filesystem files should override embedded files.
Public fieldStatic memberConfigSectionName The name of the configuration section containing the list of assemblies that should participate in the virtual filesystem.
Top
Extension Methods
 NameDescription
Public Extension MethodGetEnumValueOrDefault Gets the enumeration constant for value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions)
Public Extension MethodGetEnumValueOrDefaultT Gets the enumeration constant for this value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions)
Top
Remarks

ASP.NET retrieves files to serve via the HostingEnvironment. Rather than opening a file via File, you ask the HostingEnvironment for its VirtualPathProvider and ask that provider for the file. The provider will return a VirtualFile reference that will allow you to open a stream on the file and use the contents.

This implementation of VirtualPathProvider allows you to serve files to ASP.NET through embedded resources. Rather than deploying your web forms, user controls, etc., to the file system, you can embed the files as resources right in your assembly and deploy just your assembly. The VirtualPathProvider mechanism will take care of the rest.

Caution note  Caution
Most VirtualPathProvider implementations handle both directories and files. This implementation handles only files. As such, if the VirtualPathProvider is used to enumerate available files (as in directory browsing), files provided via embedded resource will not be included.

To use this VirtualPathProvider, you need to do four things to your web application.

First, you need to set all of the files you want to serve from your assembly as embedded resources. By default, web forms and so forth are set as "content" files; setting them as embedded resources will package them into your assembly.

Second, in your AssemblyInfo.cs file (or whichever file you are declaring your assembly attributes in) you need to add one EmbeddedResourceFileAttribute for every file you plan on serving. This lets the provider know which embedded resources are available and which are actually resources for other purposes. Your assembly attributes will look something like this:

C#
[assembly: EmbeddedResourceFileAttribute("MyNamespace.WebForm1.aspx", "MyNamespace")]
[assembly: EmbeddedResourceFileAttribute("MyNamespace.UserControl1.ascx", "MyNamespace")]

Third, you need to register this provider at application startup so ASP.NET knows to use it. In your Global.asax, during Application_OnStart, put the following:

C#
System.Web.Hosting.HostingEnvironment.RegisterVirtualPathProvider(new EmbeddedResourcePathProvider());

Fourth, in your web.config file, you need to set up a configuration section called embeddedFileAssemblies that lets the provider know which assemblies should be queried for embedded files. A sample configuration section looks like this:

C#
<configuration>
  <configSections>
    <section name="embeddedFileAssemblies" type="GSF.Configuration.StringCollectionSectionHandler, GSF.Web.Hosting.EmbeddedResourcePathProvider"/>
  </configSections>
  <embeddedFileAssemblies>
    <add value="My.Web.Assembly"/>
  </embeddedFileAssemblies>
  <!-- ... other web.config items ... -->
</configuration>

Once you have that set up, you're ready to serve files from embedded resources. Simply deploy your application without putting the embedded resource files into the filesystem. When you visit the embedded locations, the provider will automatically retrieve the proper embedded resource.

File paths are mapped into the application using the EmbeddedResourceFileAttribute declarations and the MapResourceToWebApplication(String, String) method. This allows you to set up your web application as normal in Visual Studio and the folder structure, which automatically generates namespaces for your embedded resources, will translate into virtual folders in the embedded resource "filesystem."

By default, files that are embedded as resources will take precedence over files in the filesystem. If you would like the files in the filesystem to take precedence (that is, if you would like to allow the filesystem to "override" embedded files), you can set a key in the appSettings section of your web.config file that enables overrides:

C#
<configuration>
  <!-- ... other web.config items ... -->
  <appSettings>
    <add key="GSF.Web.Hosting.EmbeddedResourcePathProvider.AllowOverrides" value="true"/>
  </appSettings>
</configuration>

For more information on virtual filesystems in ASP.NET, check out VirtualPathProvider.

See Also