Embedded
|
public class EmbeddedResourcePathProvider : VirtualPathProvider
The EmbeddedResourcePathProvider type exposes the following members.
Name | Description | |
---|---|---|
EmbeddedResourcePathProvider | Initializes a new instance of the EmbeddedResourcePathProvider class |
Name | Description | |
---|---|---|
AllowOverrides | Gets a value indicating if embedded files can be overridden. | |
Files | Gets the collection of files served by this provider. | |
Previous | Gets a reference to a previously registered VirtualPathProvider object in the compilation system. (Inherited from VirtualPathProvider) |
Name | Description | |
---|---|---|
CombineVirtualPaths | Combines a base path with a relative path to return a complete path to a virtual resource. (Inherited from VirtualPathProvider) | |
CreateObjRef | Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject) | |
DirectoryExists | Gets a value that indicates whether a directory exists in the virtual file system. (Inherited from VirtualPathProvider) | |
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object) | |
FileExists |
Gets a value that indicates whether a file exists in the virtual file system.
(Overrides VirtualPathProviderFileExists(String)) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object) | |
GetCacheDependency |
Creates a cache dependency based on the specified virtual paths.
(Overrides VirtualPathProviderGetCacheDependency(String, IEnumerable, DateTime)) | |
GetCacheKey | Returns a cache key to use for the specified virtual path. (Inherited from VirtualPathProvider) | |
GetConfiguredAssemblyNames | Gets the names of the configured assemblies from configuration. | |
GetDirectory | Gets a virtual directory from the virtual file system. (Inherited from VirtualPathProvider) | |
GetFile |
Gets a virtual file from the virtual file system.
(Overrides VirtualPathProviderGetFile(String)) | |
GetFileHash | Returns a hash of the specified virtual paths. (Inherited from VirtualPathProvider) | |
GetHashCode | Serves as the default hash function. (Inherited from Object) | |
GetLifetimeService | Retrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject) | |
GetType | Gets the Type of the current instance. (Inherited from Object) | |
Initialize |
Initializes the VirtualPathProvider instance.
(Overrides VirtualPathProviderInitialize) | |
InitializeLifetimeService | Gives the VirtualPathProvider object an infinite lifetime by preventing a lease from being created. (Inherited from VirtualPathProvider) | |
MapResourceToWebApplication | Maps an embedded resource ID into a web application relative path (~/path). | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object) | |
MemberwiseClone(Boolean) | Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject) | |
ProcessEmbeddedFiles | Reads in the embedded files from an assembly an processes them into the virtual file system. | |
ToString | Returns a string that represents the current object. (Inherited from Object) |
Name | Description | |
---|---|---|
ConfigKeyAllowOverrides | Appsettings key that indicates if filesystem files should override embedded files. | |
ConfigSectionName | The name of the configuration section containing the list of assemblies that should participate in the virtual filesystem. |
Name | Description | |
---|---|---|
GetEnumValueOrDefault |
Gets the enumeration constant for value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions) | |
GetEnumValueOrDefaultT |
Gets the enumeration constant for this value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions) |
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 |
---|
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:
[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:
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:
<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:
<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.