前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Global文件的作用

Global文件的作用

作者头像
静心物语313
发布2020-03-24 11:04:49
1.6K0
发布2020-03-24 11:04:49
举报

1.Session_Start()和Session_End(). 2.进程外的Session不会触发Session_End()事件 3.重点:Application_Start.Application_BeginRequest.Application_Error.

4.UrlRewrite:

代码语言:javascript
复制
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,名字也是定死的

代码语言:javascript
复制
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文件

代码语言:javascript
复制
<%@ 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

代码语言:javascript
复制
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

代码语言:javascript
复制
        //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");
            }

优化后效果图

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

调试信息

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • urlrewrite()案例
  • 实验目的:
    • ListPeople.aspx文件
      • ListPeople.cs
        • 修改全局文件Global.asax
        • 优化后效果图
        • 调试信息
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档