/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
///get source at http://www.codeproject.com/KB/aspnet/ASP2UserControlLibrary.aspx
///
/// all VirtualPathProvider will be construct a chain with the _previous member,see HostingEnvironment.RegisterVirtualPathProviderInternal(VirtualPathProvider virtualPathProvider)
///
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Hosting;
using System.IO;
using System.Reflection;
namespace Everest.Library.Mvc.View{
public class AssemblyResourceProvider : System.Web.Hosting.VirtualPathProvider
{
public AssemblyResourceProvider() { }
private bool IsAppResourcePath(string virtualPath)
{
String checkPath =
VirtualPathUtility.ToAppRelative(virtualPath);
return checkPath.StartsWith("~/App_Resource/", StringComparison.InvariantCultureIgnoreCase);
}
public override bool FileExists(string virtualPath)
{
return (IsAppResourcePath(virtualPath) || base.FileExists(virtualPath));
}
public override VirtualFile GetFile(string virtualPath)
{
if (IsAppResourcePath(virtualPath))
return new AssemblyResourceVirtualFile(virtualPath);
else
return base.GetFile(virtualPath);
}
public override System.Web.Caching.CacheDependency GetCacheDependency(string virtualPath, System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart)
{
if (IsAppResourcePath(virtualPath))
return new System.Web.Caching.CacheDependency(Assemblies.AssemblyResolver.Packages.DirectoryPath);
else
return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
}
}
class AssemblyResourceVirtualFile : VirtualFile
{
string path;
public AssemblyResourceVirtualFile(string virtualPath)
: base(virtualPath)
{
path = VirtualPathUtility.ToAppRelative(virtualPath);
}
public override System.IO.Stream Open()
{
System.IO.Stream stream = null;
string[] parts = path.Split('/');
string assemblyName = parts[2];
string resourceName = parts[3];
System.Reflection.Assembly assembly = Assemblies.AssemblyResolver.ResolveAssembly(assemblyName);
if (assembly != null)
{
stream = assembly.GetManifestResourceStream(resourceName);
}
if (stream == null)
{
throw new EmbeddedViewNotFound(string.Format("Embedded view resource not found.\nResourceName:{0},assembly name:{1}", resourceName, assemblyName));
}
return stream;
}
}
}
|