zoukankan      html  css  js  c++  java
  • 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如果没有后台代码,可以使用基页面。

  • 相关阅读:
    Hadoop学习------Hadoop安装方式之(一):单机部署
    Linux系统上安装、卸载JAVA、TOMCAT的方法
    在Linux系统上安装Oracle数据库
    C:Program Files (x86)MSBuild14.0inMicrosoft.Common.CurrentVersion.targets(4714,5): error MSB30...
    软件设计,数据库结构设计,设计思想
    面试题 SqlServer知识
    @Ajax.ActionLink跳转页面的问题解决方案 MVC Ajax不支持问题
    .net机试题总结
    .Net机试题——编写一个BS架构的多层表结构的信息管理模块
    C# 不同类型对象同名属性赋值
  • 原文地址:https://www.cnblogs.com/forhell/p/4970283.html
Copyright © 2011-2022 走看看