VirtualPathProvider的使用
2015-11-16 22:32
393 查看
解决的问题,加载数据库中的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如果没有后台代码,可以使用基页面。
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如果没有后台代码,可以使用基页面。
相关文章推荐
- 线程安全
- 从巴黎的恐怖袭击看以色列如何用大数据进行反恐
- POI操作Excel设置前景色背景色
- POI操作Excel设置前景色背景色
- POI操作Excel设置前景色背景色
- 好用的vimrc
- 1029 Median
- 夯实JAVA基本之一 —— 泛型详解(1):基本使用
- 使用ListView时的几个BUG与相应的优化
- Android平板上开发应用的一点心得——精确适配不同的dpi和屏幕尺寸
- 2015年11月16日
- hdu 1022 Train Problem I(栈的模拟)
- 在.NET开发中的单元测试工具之(1)——NUnit
- UI 第五天 代理设计模式 手势识别器
- 20135203齐岳信息安全系统设计基础——实验一实验报告
- 自动填充短信验证码(使用ContentObserver)
- garch matlab
- 10月月赛
- linux下rsync的配置以实现文件夹的同步及常见error总结
- 笔记十(重要公式阶段总结)