Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6843189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:12:55+00:00 2026-05-27T00:12:55+00:00

I’m trying to implement storing an Azure web site’s Themes in the Azure CDN.

  • 0

I’m trying to implement storing an Azure web site’s Themes in the Azure CDN.

I’ve copied the files into the CDN retaining the folder structure as it was in the original App_Themes folder.

I’ve created a VirtualPathProvider and the necessary Virtualdirectory and VirtualFile classes. The provider is registered in global.asax.

My problem is that the only file that seems to come from the CDN is the skin file. All the images, css etc are still refernced as if they are in the standard App_Themes structure. If I put a break point in my code then my VirtualTheme’s Open method is only ever called for the skin file.

Has anyone managed to implement a solution like this?

Any ideas what I’m doing wrong?

[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Medium)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.High)]
public class RedirectAppThemes : VirtualPathProvider
{

    private bool IsPathVirtual(string virtualPath)
    {
        String checkPath = VirtualPathUtility.ToAppRelative(virtualPath);
        return checkPath.StartsWith("~/App_Themes/", StringComparison.InvariantCultureIgnoreCase);
    }

    public override bool FileExists(string virtualPath)
    {
        if (IsPathVirtual(virtualPath))
        {
            VirtualThemeFile file = new VirtualThemeFile(virtualPath);
            return file.Exists;
        }
        else
        {
            return Previous.FileExists(virtualPath);
        }
    }

    public override VirtualFile GetFile(string virtualPath)
    {
        if (IsPathVirtual(virtualPath))
        {
            return new VirtualThemeFile(virtualPath);
        }
        else
        {
            return Previous.GetFile(virtualPath);
        }
    }

    public override bool DirectoryExists(string virtualDir)
    {
        if (IsPathVirtual(virtualDir))
        {
            VirtualThemeDirectory dir = new VirtualThemeDirectory(virtualDir);
            return dir.Exists;
        }
        else
        {
            return Previous.DirectoryExists(virtualDir);
        }
    }

    public override VirtualDirectory GetDirectory(string virtualDir)
    {
        if (IsPathVirtual(virtualDir))
        {
            return new VirtualThemeDirectory(virtualDir);
        }
        else
        {
            return Previous.GetDirectory(virtualDir);
        }
    }

    public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
    {
        if (IsPathVirtual(virtualPath))
        {
            return null;
        }
        else
        {
            return Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
        }
    }


}

 [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class VirtualThemeDirectory : VirtualDirectory
{
    private string cdnPath = "http://xxxxxxxxx.blob.core.windows.net/";

    public VirtualThemeDirectory(string virtualPath) : base(virtualPath)
    {
    }

    public override IEnumerable Children
    {
        get
        {
            List<object> children = new List<object>();

            string dir = this.VirtualPath.Replace("/App_Themes/", String.Empty);
            CloudBlobClient client = new CloudBlobClient(cdnPath);

            var blobs = client.ListBlobsWithPrefix(@"webinterfacethemes/" + dir );

            foreach (CloudBlobDirectory directory in blobs.OfType<CloudBlobDirectory>())
            {
                VirtualThemeDirectory vtd = new VirtualThemeDirectory(directory.Uri.AbsolutePath.Replace("/webinterfacethemes/", "/App_Themes/"));
                children.Add(vtd);
            }
            foreach (CloudBlob file in blobs.OfType<CloudBlob>())
            {
                VirtualThemeFile vtf = new VirtualThemeFile(file.Uri.AbsolutePath.Replace("/webinterfacethemes/", "/App_Themes/"));
                children.Add(vtf);
            }                

            return children;
        }
    }

    public override IEnumerable Directories
    {
        get
        {
            List<object> children = new List<object>();

            string dir = this.VirtualPath.Replace("/App_Themes/", String.Empty);
            CloudBlobClient client = new CloudBlobClient(cdnPath);

            var blobs = client.ListBlobsWithPrefix(@"webinterfacethemes/" + dir);

            foreach (CloudBlobDirectory directory in blobs.OfType<CloudBlobDirectory>())
            {
                VirtualThemeDirectory vtd = new VirtualThemeDirectory(directory.Uri.AbsolutePath.Replace("/webinterfacethemes/", "/App_Themes/"));
                children.Add(vtd);
            }

            return children;
        }
    }

    public override IEnumerable Files
    {
        get
        {
            List<object> children = new List<object>();

            string dir = this.VirtualPath.Replace("/App_Themes/", String.Empty);
            CloudBlobClient client = new CloudBlobClient(cdnPath);

            var blobs = client.ListBlobsWithPrefix(@"webinterfacethemes/" + dir);

            foreach (CloudBlob file in blobs.OfType<CloudBlob>())
            {
                VirtualThemeFile vtf = new VirtualThemeFile(file.Uri.AbsolutePath.Replace("/webinterfacethemes/", "/App_Themes/"));
                children.Add(vtf);
            }

            return children;
        }
    }

    public bool Exists
    {
        get
        {
            string dir = this.VirtualPath.Replace("/App_Themes/", String.Empty);
            CloudBlobClient client = new CloudBlobClient(cdnPath);

            if (client.ListBlobsWithPrefix("webinterfacethemes/" + dir).Count() > 0)
                return true;
            else
                return false;

        }
    }
}

[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class VirtualThemeFile : VirtualFile
{
    private string cdnPath = "http://xxxxxxx.vo.msecnd.net/webinterfacethemes/";

    public VirtualThemeFile(string VirtualPath) : base(VirtualPath)
    {

    }

    public override Stream Open()
    {
        string url = this.VirtualPath.Replace("/App_Themes/", cdnPath);
        HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
        WebResponse myResp = myReq.GetResponse();
        Stream stream = myResp.GetResponseStream();
        return stream;
    }

    public bool Exists
    {
        get
        {
            //Check if the file exists
            //do this with a HEAD only request so we don't download the whole file
            string url = this.VirtualPath.Replace("/App_Themes/", cdnPath);
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
            myReq.Method = "HEAD";
            HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
            if (myResp.StatusCode == HttpStatusCode.OK)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

And in Global.asax Application_start:

RedirectAppThemes redirect = new RedirectAppThemes();
        HostingEnvironment.RegisterVirtualPathProvider(redirect);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:12:56+00:00Added an answer on May 27, 2026 at 12:12 am

    Mucho Googling eventually found this similar problem

    I’ve added the following to my web.config inside the <system.webServer> tags and now the image and css files are calling the methods in my code.

    <handlers>
      <add name="Images" path="*.png" verb="GET,HEAD,POST" type="System.Web.StaticFileHandler" modules="ManagedPipelineHandler" resourceType="Unspecified" />
      <add name="Stylesheets" path="*.css" verb="GET,HEAD,POST" type="System.Web.StaticFileHandler" modules="ManagedPipelineHandler" resourceType="Unspecified" />
    </handlers>
    

    If anyone is looking for a complete solution to putting their Themes in the CDN using this method I’ve also changed the following from the original code posted above and it now works:

    the VirtualFile class is as follows:

    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class VirtualThemeFile : VirtualFile
    {
        private string cdnPath = "https://xxxxxx.vo.msecnd.net/webinterfacethemes/";
        private string blobURL;
    
        public VirtualThemeFile(string VirtualPath) : base(VirtualPath)
        {
            blobURL = this.VirtualPath.Replace("/App_Themes/", cdnPath);
        }
    
        public override Stream Open()
        {
            CloudBlobClient client = new CloudBlobClient(cdnPath);
            CloudBlob blob = client.GetBlobReference(blobURL);
            MemoryStream stream = new MemoryStream();
            blob.DownloadToStream(stream);
            stream.Seek(0, SeekOrigin.Begin);
            return stream;
        }
    
        public bool Exists
        {
            get
            {
                CloudBlobClient client = new CloudBlobClient(cdnPath);
                CloudBlob blob = client.GetBlobReference(blobURL);
                try
                {
                    blob.FetchAttributes();
                    return true;
                }
                catch (StorageClientException e)
                {
                    if (e.ErrorCode == StorageErrorCode.ResourceNotFound)
                    {
                        return false;
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
    }
    

    And I’ve changed my global.asax code so that the VirtualPathProvider is invoked if the site is pre-compiled:

    HostingEnvironment hostingEnvironmentInstance = (HostingEnvironment)typeof(HostingEnvironment).InvokeMember("_theHostingEnvironment", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetField, null, null, null);
            MethodInfo mi = typeof(HostingEnvironment).GetMethod("RegisterVirtualPathProviderInternal", BindingFlags.NonPublic | BindingFlags.Static);
            mi.Invoke(hostingEnvironmentInstance, new object[] { new RedirectAppThemes() });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.