VirtualPathProvider的使用
解决的问题,加载数据库中的UI
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
DbPathProvider provider = new DbPathProvider();
HostingEnvironment.RegisterVirtualPathProvider(provider);
}
}
public class DbPathProvider : VirtualPathProvider
{
protected bo_view cbo_view = null;
Dictionary<string, FakeCacheDependency> dicFakeCacheDependencys = new Dictionary<string, FakeCacheDependency>();
public DbPathProvider() : base()
{
}
public override string GetCacheKey(string virtualPath)
{
string mvirtualPath = getmvirtual(virtualPath);
ICriteria<bo_view> wbo_view = CriteriaFactory.Create<bo_view>();
wbo_view = wbo_view.Where(q => q.pagename == mvirtualPath);
cbo_view = bo_view.Fetch(wbo_view);
if (!cbo_view.IsNew)
{
FakeCacheDependency fdc = null;
if (dicFakeCacheDependencys.TryGetValue(cbo_view.pagename, out fdc))
{
fdc.Clear(cbo_view.operdate);
}
}
return base.GetCacheKey(virtualPath);
}
public override System.Web.Caching.CacheDependency GetCacheDependency(string virtualPath, System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart)
{
if (cbo_view.IsNew)
return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
dicFakeCacheDependencys[cbo_view.pagename] = new FakeCacheDependency(cbo_view.operdate);
return dicFakeCacheDependencys[cbo_view.pagename];
}
public class FakeCacheDependency : System.Web.Caching.CacheDependency
{
public FakeCacheDependency(DateTime lastModified)
{
base.SetUtcLastModified(lastModified);
}
public FakeCacheDependency()
{
base.SetUtcLastModified(DateTime.UtcNow);
}
public void Clear(DateTime lastModified)
{
if (this.UtcLastModified != lastModified)
{
base.SetUtcLastModified(lastModified);
NotifyDependencyChanged(this, EventArgs.Empty);
}
}
}
public override bool DirectoryExists(string virtualDir)
{
if (cbo_view.IsNew)
base.DirectoryExists(virtualDir);
return true;
}
public override bool FileExists(string virtualPath)
{
if (cbo_view.IsNew)
base.FileExists(virtualPath);
return true;
}
public override VirtualFile GetFile(string virtualPath)
{
if (cbo_view.IsNew)
{
return this.Previous.GetFile(virtualPath);
}
return new DbVirtualFile(virtualPath, cbo_view);
}
private static string getmvirtual(string virtualPath)
{
string mvirtualPath = virtualPath;
int fileindex = virtualPath.LastIndexOf('/');
if (fileindex >= 0)
{
mvirtualPath = virtualPath.Substring(fileindex);
}
return mvirtualPath;
}
}
public class DBCacheDependency : CacheDependency
{
public DBCacheDependency(string path) : base(HttpRuntime.AppDomainAppPath.TrimEnd('\\') + "\\web.config")
{
FinishInit();
}
public new bool HasChanged
{
get { return true; }
}
static string GetDependKey()
{
return System.Guid.NewGuid().ToString();
}
}
public class DbVirtualFile : VirtualFile
{
private static string getmvirtual(string virtualPath)
{
string mvirtualPath = virtualPath;
int fileindex = virtualPath.LastIndexOf('/');
if (fileindex >= 0)
{
mvirtualPath = virtualPath.Substring(fileindex);
}
return mvirtualPath;
}
protected bo_view cbo_view = null;
public DbVirtualFile(string filename, bo_view mbo_view) : base(filename)
{
cbo_view = mbo_view;
}
public override Stream Open()
{
if (cbo_view.IsNew)
{
throw new Exception("此资源不存在");
}
byte[] bytes = System.Text.Encoding.Default.GetBytes(cbo_view.viewcontent);
return new System.IO.MemoryStream(bytes);
}
}
关键点:
在创建ui的时候,要注意@ page指令的写法
<%@ Page Title="" Language="C#" MasterPageFile="~/admin/Admin.Master" AutoEventWireup="true" CodeBehind="mm.aspx.cs" Inherits="MyBasePage" CompilationMode="Always" %> CodeBedhid可以是不存在的,YHWebCore.Business.MyBasePage如果没有后台代码,可以使用基页面。