• .net学习之母版页执行顺序、jsonp跨域请求原理、IsPostBack原理、服务器端控件按钮Button点击时的过程、缓存、IHttpModule 过滤器


    1.WebForm使用母版页后执行的顺序是先执行子页面中的Page_Load,再执行母版页中的Page_Load,请求是先生成母版页的控件树,然后将子页面生成的控件树填充到母版页中,最后输出

    2.WebForm使用母版页进行登录时候的验证

    //新建一个页面父类
    public abstract class BasePage:Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                //1.身份验证的方法 Session
                if (Session["uinfo"] == null)
                {
                    if (Request.Cookies["uinfo"] == null)
                    {
                        PageHelper.WriteJsMsg("您尚未登录~~~", "/Manage/View/Login.html");
                        Response.End();
                    }
                    else
                    {
                        //如果Cookie中有用户id,则 根据id读取用户对象 存入 Session
                        string strId = Request.Cookies["uinfo"].Value;
                        //验证 strId 是否为 整型
                        //验证成功则读取数据库 , 并将 用户对象 保存到Session
                        Session["uinfo"] = new BLL.Users().GetModel(int.Parse(strId));
    
                        //2.调用子类的 重写方法
                        ChildPageLoad();
                    }
                }
    
              
            }
    
            public abstract void ChildPageLoad();
        }
    
    //页面需要权限验证的都继承BasePage
    public partial class WebForm3 :BasePage
        {
          
            public override void ChildPageLoad()
            {
                throw new NotImplementedException();
            }
        }

    3.jsonp跨域请求原理,就是使用script标签可以跨域,请求回来的字符串浏览器作为js代码进行执行
    jQuery请求的代码:

    $.ajax("url",
        type:"get",
        dataType:"jsonp",
        jsonp:"callback",//发送服务器的回调函数名参数
        jsonpCallback:"callbackFun",//指定回调的函数名
        success:function(){
            alert("请求成功");
        }
    );
    function callbackFun(data){
        
    }

    4.IsPostBack原理:
    是否为表单提交,服务器端asp.net框架依靠检测请求报文中的_VIEWSTATE来设置值

    5.服务器端控件按钮Button点击时的过程
    第8个事件创建前台页面对象,11、12之间调用页面的ProccessRequest方法,打造页面控件树,调用PageLoad,处理非按
    钮点击事件,处理按钮点击事件,Render生成html代码

    浏览器端有按钮被点击的时候,会生成请求报文到服务器,被点击按钮的name会被传到服务器,服务器端asp.net框架会
    根据这个name到控件树中找到对应的服务器控件,并执行它的事件,从而调用事件方法,但是,如果请求报文中包含多个
    按钮名字,则服务器端只执行最后一个按钮的事件方法。

    6.缓存
    (1)ASP.NET 页面输出缓存
    <%@ OutputCache Duration="30" VaryByParam="id;name"%>
    <%--VaryByParam 必需属性,可以为 VaryByParam="none" 或 VaryByParam="*"
    或指定参数,如 VaryByParam="PcacheTime",VaryByParam="PcacheTime;page"--%>

    (2)自定义缓存

    protected void Page_Load(object sender, EventArgs e)
            {
                if (Cache["myDog"] != null)
                {
                    string str = Cache["myDog"].ToString();
                    Response.Write(str);
                }
                else
                {
                    //1.设置永久 缓存数据
                   // Cache["myDog"] = "我的小狗叫 花花 ~~~";
    
                    //2.设置 相对过期时间为10秒的 缓存数据,如果一直有请求,就在当前的请求上重新推迟10秒
                    //Cache.Add("myDog", "我的小狗叫 花花 ~~~", null, System.Web.Caching.Cache.NoAbsoluteExpiration,
                    //    new TimeSpan(0, 0, 10), System.Web.Caching.CacheItemPriority.Default, null);
                    //3.设置 绝对过期时间为10秒后的 缓存数据,不管有没有一直请求,都是在创建10秒后过期
                    //Cache.Add("myDog", "我的小狗叫 花花 ~~~", null, DateTime.Now.AddSeconds(10), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default,OnRemoveCallback);
    
                    //4.为 缓存项 添加 回调函数(当缓存被 销毁的时候 执行),带 【文件缓存依赖】
                    //CacheDependency cd = new CacheDependency(Request.MapPath("/data.txt"));
                    //Cache.Add("myDog", "我的小狗叫 花花 ~~~", cd, Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, OnRemoveCallback);
    
                    //5.缓存依赖数据库 具体代码参考 http://adyhpq.blog.163.com/blog/static/3866700201082624615851/
                    SqlCacheDependency sqlDep = new SqlCacheDependency("LWord", "Users");
    
                    IList<LeaveWordBorad.MODEL.Users> list = new LeaveWordBorad.BLL.Users().GetList();
    
                    Cache.Add("myDog", list.Count, sqlDep, Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, OnRemoveCallback);
    
                    Response.Write("刚买了只狗~~~!");
                }
            }
    
            /// <summary>
            /// 缓存过期之后执行的函数
            /// </summary>
            /// <param name="key"></param>
            /// <param name="value"></param>
            /// <param name="reason"></param>
            private void OnRemoveCallback(string key, object value, System.Web.Caching.CacheItemRemovedReason reason)
            {
                string path = Server.MapPath("/log.txt");
                System.IO.File.AppendAllText(path, key + "=" + value + "reason=" + reason);
            }

    7.IHttpModule 过滤器

     public class MyHttpModule:IHttpModule
        {
        //每次请求的时候都会调用该方法,可以在这里进行url重写
            public void Init(HttpApplication context)
            {
                context.BeginRequest += context_BeginRequest;
            }
    
            void context_BeginRequest(object sender, EventArgs e)
            {
                HttpContext context =( (HttpApplication) sender).Context;
                context.Response.Write("哈哈"+DateTime.Now);
            }
    
            public void Dispose()
            {
               
            }
         }
    Web.config
    
    <system.webServer>
      <modules>
        <add name="myHttpModule" type="WebApplication2.MyHttpModule"/>
      </modules>
    </system.webServer>
    
    Global文件:
      protected void Application_BeginRequest(object sender, EventArgs e)
            {
            //可以在这里进行url重写
                Response.Write("Application_BeginRequest哈哈");
            }
    protected void Application_BeginRequest(object sender, EventArgs e)
            {
                string requestUrl = Request.RawUrl;///WebForm1/8
                Response.Write(requestUrl);
                string[] strUrlParts = requestUrl.Split(new char[1]{'/'},StringSplitOptions.RemoveEmptyEntries);
                if (strUrlParts.Count()>0)
                {
                    if (strUrlParts[0].Equals("WebForm1", StringComparison.InvariantCultureIgnoreCase))
                    {
                        HttpContext.Current.RewritePath("/WebForm1.aspx?id=" + strUrlParts.Last());
                    }
                    
                }
            }
  • 相关阅读:
    javascript、jQuery的扩展方法,扩展实例展示代码
    经典弹出层Colorbox
    在Entity Framework中使用事务
    WCF中常用的binding方式
    前端上传组件Plupload使用指南
    SQL ROW_NUMBER() OVER函数的基本用法用法
    数据库选项--触发器
    数据库选项--会刷新过程缓存的选项
    数据库选项--页面检查
    数据库选项--自动更新统计
  • 原文地址:https://www.cnblogs.com/yxlblogs/p/3799325.html
Copyright © 2020-2023  润新知