Global文件的作用

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");
            }

优化后效果图

这里写图片描述

调试信息

这里写图片描述
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静心物语313

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值