1.Session_Start()和Session_End(). 2.进程外的Session不会触发Session_End()事件 3.重点:Application_Start.Application_BeginRequest.Application_Error.
4.UrlRewrite:
1.view.asp?id=1---->View-1.aspx 2.在BeginRequest中获取请求的url (HttpContext.Current.Request.RawUrl).生成真正的地址(Context.RewriterPath()) 3.静态文件等默认是不经过asp.net引擎处理的,因此不会经过Global。
5.匹配这个ViewPerson-1.aspx 6.Regex.Match(Context.Request.Path,@”^\ViewPerson\-(\d+).aspx”) 1) “ ^ ”表示以什么字符开头; 2) “ - ”C#中的有特殊含义,同时在正则表达式中有特殊含义,使用了两个“ \ ”转义;C#中的也可以在字符串前加” @ “符号 @”匹配的字符串” ; 3) 对整个字符串的匹配是第0组、对第一个圆括号的匹配为第1组匹配 ; 4) 对数字的匹配是\d、对个数字为\d+ ; 5) “ . ”在正则表达式中有特殊含义,使用了一个“ \ ”转义; 6) 匹配的字符串结尾用“ ”符号结束。“匹配的字符串 ”符号结束。 “匹配的字符串” 7. 右键项目—》全局应用程序类—》Global.asax 注意:Global是定死的名字,不能修改为其他的。类似的一个文件时Web.config,名字也是定死的
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Web.Security; using System.Web.SessionState; namespace wj { public class Global : System.Web.HttpApplication { //1.---------------自从服务器启动起来,网站第一次被访问的时候Application_Start执行 protected void Application_Start(object sender, EventArgs e) { File.AppendAllText("d:\\1.txt", "记录日志时间为:" + DateTime.Now + "启动了这个方法:Application_Start \r\n"); } //Session启动的时候 protected void Session_Start(object sender, EventArgs e) { } //2.--------------- 当一个请求过来的时候,这个请求访问的页面必须是动态的页面 ashx 或者 aspx 结尾的 ,访问html等静态的页面时iis服务器直接把文件给浏览器,不经过asp.net引擎的处理的。 protected void Application_BeginRequest(object sender, EventArgs e) { File.AppendAllText("d:\\1.txt", "记录日志时间为:" + DateTime.Now + "启动了这个方法:Application_BeginRequest" +"当前请求地址是:"+Context.Request.RawUrl+"\r\n"); } protected void Application_AuthenticateRequest(object sender, EventArgs e) { } //3.--------------程序中发生未处理的异常 protected void Application_Error(object sender, EventArgs e) { //记录错误日志文件 } //session过期(只有是进程捏的Session,也就是InProc过期的时候才调用Session_End方法) protected void Session_End(object sender, EventArgs e) { } protected void Application_End(object sender, EventArgs e) { } } }
改写请求地址,加快了搜索的几率,在SEO中涉及,非动态的页面搜索引擎更容搜索 到;
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListPeople.aspx.cs" Inherits="wj.ListPeople" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> 用户名:<asp:TextBox ID="user" runat="server"></asp:TextBox> <br /> 密码:<asp:TextBox ID="pwd" runat="server"></asp:TextBox> <br /> <asp:Label ID="xs" runat="server"></asp:Label> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace wj { public partial class ListPeople : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { long id = Convert.ToInt64(Request["id"]); DataTable dt = SqlHelper.ExecuteQuery("select * from T_users where id=@id",new SqlParameter("@id",id)); if (dt.Rows.Count <= 0) { xs.Text = "查无此人!"; } else { DataRow row=dt.Rows[0]; user.Text=(string)row["username"]; pwd.Text=(string)row["password"]; } } } }
//2.--------------- 当一个请求过来的时候,这个请求访问的页面必须是动态的页面 ashx 或者 aspx 结尾的 ,访问html等静态的页面时iis服务器直接把文件给浏览器,不经过asp.net引擎的处理的。 protected void Application_BeginRequest(object sender, EventArgs e) { //即使访问了一个不存在的页面也会通过这个文件 File.AppendAllText("d:\\1.txt", "记录日志时间为:" + DateTime.Now + "启动了这个方法:Application_BeginRequest" +"当前请求地址是:"+Context.Request.RawUrl+"\r\n"); //使用urlRewrite重写请求地址 /ListPeople.aspx?id=18 改成这个样子的 ListPeople.aspx-1.aspx" Match match = Regex.Match(Context.Request.Path, "^/ListPeople\\-(\\d+)\\.aspx");//Path获取当前的虚拟路径" /ListPeople.aspx " if (match.Success) { string id = match.Groups[1].Value; Context.RewritePath("/ListPeople.aspx?id="+id); } else { Context.RewritePath("/error.html"); }
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。
我来说两句