• 数据缓存依赖、图片防盗链 url重写


    数据库缓存依赖
    1、数据库缓存依赖
    -S服务器名称  -E集成身份验证  -ed启动 -d数据库名称  -et指定缓冲依赖的表名 -t表名
    在vs2010的命令提示符中运行(切换到aspnet_regsql.exe所在的目录)
    aspnet_regsql.exe -S 127.0.0.1 -E -ed -d myphotos -et -t photos

    缓存依赖禁用该数据库
    aspnet_regsql -S . -E -dd -d myphotos

    2、依赖于数据库的web.config配置
    <system.web>
        <caching>
          <sqlCacheDependency enabled="true">
            <databases>
              <add name="myphotos" connectionStringName="connectionString" pollTime="500"/>
            </databases>
          </sqlCacheDependency>
        </caching>

    3、 System.Web.Caching.SqlCacheDependency dep = new System.Web.Caching.SqlCacheDependency("myphotos", "photos");
                    Cache.Insert("list", list, dep);


    进程外session
        StateServer  对象必须可序列化
            1.开始 ASP.NET 状态服务  services.msc
            2.配置webconfig
                <sessionState mode="StateServer" stateConnectionString="tcpip=localhost:42424"></sessionState>
            //





            3.如果是远程的stateserver配置注册表  regedit
                HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesaspnet_stateParametersAllowRemoteConnection
                设置成1
        




    图片防止盗链

     void Application_BeginRequest(object sender, EventArgs e)
        {
            if (Request.RawUrl.Contains("images/"))
            {
                if (Request.UrlReferrer == null || !IsSameDomain(Request.UrlReferrer, Request.Url))
                {
                    ////
                    Response.ContentType = "image/jpeg";
                    string path = Request.MapPath("~/daolian.jpg");
                    Response.WriteFile(path);
                    //结束请求
                    Response.End();
                }
            }
        }
        //判断两个域名是否相等
        bool IsSameDomain(Uri u1,Uri u2)
        {
            return Uri.Compare(u1, u2, UriComponents.HostAndPort, UriFormat.SafeUnescaped, StringComparison.CurrentCultureIgnoreCase) == 0 ? true : false;
        }










    url重写


    一、原理
    void Application_BeginRequest(object sender, EventArgs e)
        {
         //url重写
            HttpApplication app = sender as HttpApplication;
            string url = app.Request.RawUrl;
            Regex r = new Regex("/(\d+)/details\.htm",RegexOptions.IgnoreCase);
            Match m = r.Match(url);
            if (m.Success)
            {
                string id = m.Groups[1].Value;
                app.Context.RewritePath("~/PhotoDetails.aspx?id=" + id);
            }
        }

     二、urlRewriter
    1、在<configSections>节点加入
     <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
    2、在</configSections>之后加入
        
      <RewriterConfig>
        <Rules>
          <RewriterRule>
            <LookFor>~/(d{4})/(d{2})/Default.aspx</LookFor>
            <SendTo>~/Default.aspx?ID=$1</SendTo>
          </RewriterRule>
        </Rules>
      </RewriterConfig>
    3、<httpHandlers>中加入
    <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
    或者 <httpModules>加入
    <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />











  • 相关阅读:
    VM环境安装Linux系统
    设置ShowDialog
    sqlserver同步表的脚本
    C#winform设置DateTimePicker的时间格式
    C#winform解析marc显示在datagridview中以及marc卡片显示
    C#实现图书馆程序导入ISO-2709格式(MARC)功能
    时间格式转换+固定字段加空格
    sqlserver 保留小数方法
    winform的datagridview单元格输入限制和右键单击datagridview单元格焦点跟着改变
    怎么查看那个端口被占用
  • 原文地址:https://www.cnblogs.com/eric-gms/p/3473543.html
Copyright © 2020-2023  润新知