<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Legomaster Building and Coding &#187; ASP.NET</title>
	<atom:link href="http://legomaster.net/categories/aspnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://legomaster.net</link>
	<description>Building Better Code</description>
	<lastBuildDate>Thu, 23 Jun 2011 01:00:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>VirtualPathProvider for Assemblies</title>
		<link>http://legomaster.net/2009/05/virtualpathprovider-for-assemblies/</link>
		<comments>http://legomaster.net/2009/05/virtualpathprovider-for-assemblies/#comments</comments>
		<pubDate>Sat, 02 May 2009 11:53:15 +0000</pubDate>
		<dc:creator>travis</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://legomaster.net/?p=24</guid>
		<description><![CDATA[The VirtualPathProvider class provides some options when building websites. One of the most common is to be able to server ASPX/ASCX requests from embedded resources in an assembly. A good place to start is from a CodeProject article that address this issue. First, instead of using hardcode assemblies/paths/etc, a few properties were attached. Also, instead [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://msdn.microsoft.com/en-us/library/system.web.hosting.virtualpathprovider.aspx">VirtualPathProvider</a> class provides some options when building websites. One of the most common is to be able to server ASPX/ASCX requests from embedded resources in an assembly.</p>
<p>A good place to start is from a <a title="Load WebForms and UserControls from Embedded Resources" href="http://www.codeproject.com/KB/aspnet/ASP2UserControlLibrary.aspx">CodeProject article</a> that address this issue.</p>
<p>First, instead of using hardcode assemblies/paths/etc, a few properties were attached.</p>
<pre class="brush: csharp; title: ; notranslate">        #region Properties
        public String VirtualPath { get; set; }
        public String VirtualFileSystemAssembly { get; set; }
        public String VirtualFileSystemBaseNamespace { get; set; }
        #endregion</pre>
<p>Also, instead of just assuming that a file might exist in the assembly if the virtual paths match &#8211; because we might want to serve file from multiple assemblies on the same virtual path &#8211; the code will check if the resource exists in the assembly. </p>
<pre class="brush: csharp; title: ; notranslate">        private bool DoesExistInVirtualPath(string virtualPath)
        {
            if (!IsInVirtualPath(virtualPath))
                return false;

            Assembly assembly = GetAssemblyReference();

            if (assembly != null)
            {
                String[] resources = assembly.GetManifestResourceNames();
                String resourceName = ConstructAssemblyResourceName(virtualPath);

                if (resources != null &amp;&amp; resources.Contains(resourceName, StringComparer.InvariantCultureIgnoreCase))
                    return true;
            }
            return false;
        }</pre>
<p>First we check to see if the path even matches, failing that it&#8217;s obvious that there isn&#8217;t a match. They we attempt to get a reference to the desired assembly. Of course if that fails then there&#8217;s no way to be able to serve files from it. Lastly we check the manifest for resources and if there are any does it contain the file we&#8217;re looking for. A file name mangling needs to occur (in ConstructAssemblyResourceName) because of the way the files are named as resources. </p>
<p>There are a couple of helper methods used in the above block, they are just there to keep things neat. </p>
<pre class="brush: csharp; title: ; notranslate">        private String ConstructAssemblyResourceName(string resourcePath)
        {
            // nix the base path
            String resourceName = resourcePath.Remove(0, VirtualPath.Length);
            // tack on the base namespace
            if (VirtualFileSystemBaseNamespace.Length &gt; 0)
            {
                resourceName = VirtualFileSystemBaseNamespace + '/' + resourceName;
            }
            // replace directory seperators with .'s
            resourceName = resourceName.Replace('/', '.');
            return resourceName;
        }

        private Assembly GetAssemblyReference()
        {
            if (assemblyReference != null)
                return assemblyReference;

            String binDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            String assemblyName = Path.Combine(binDir, VirtualFileSystemAssembly);

            Assembly assembly;
            try
            {
                assembly = Assembly.LoadFile(assemblyName);
            }
            catch
            {
                return null;
            }

            // if it is a real assembly - keep it
            if (assembly != null)
                assemblyReference = assembly;

            return assembly;
        }</pre>
<p>Download the source code: <a href="http://legomaster.net/wp-content/uploads/2009/05/legomasterassemblypathprovider-20090502.zip">Legomaster.AssemblyPathProvider</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://legomaster.net/2009/05/virtualpathprovider-for-assemblies/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

