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)
{
}
}
}
複制
urlrewrite()案例
實驗目的:
改寫請求位址,加快了搜尋的幾率,在SEO中涉及,非動态的頁面搜尋引擎更容搜尋 到;
ListPeople.aspx檔案
<%@ 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>
複制
ListPeople.cs
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"];
}
}
}
}
複制
修改全局檔案Global.asax
//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");
}
複制
優化後效果圖
