.Net Global.asax、httpModules伪静态功能实现

简介: //第一种方法,Global文件重写   protected void Application_Start(object sender, EventArgs e)         {             File.

//第一种方法,Global文件重写

  protected void Application_Start(object sender, EventArgs e)
        {
            File.AppendAllText(Server.MapPath("global.txt"),DateTime.Now.ToString()+" AppStart\r\n");
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            File.AppendAllText(Server.MapPath("global.txt"), DateTime.Now.ToString()+" ip:"+Request.UserHostAddress+ " SessionStart \r\n");
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            //每次打开网页都会发生
            //File.AppendAllText(Server.MapPath("global.txt"), DateTime.Now.ToString() + " ip:" + Request.UserHostAddress + "request\r\n");
            ////可以用来屏蔽指定ip
            //if (HttpContext.Current.Request.UserHostAddress == "127.0.0.1")
            //{
            //    HttpContext.Current.Response.Write("已被屏蔽");
            //    HttpContext.Current.Response.End();
            //}
            //防止图片盗链
            if (HttpContext.Current.Request.Url.AbsolutePath.EndsWith(".jpg") && HttpContext.Current.Request.UrlReferrer.Host != "localhost")
            {
                //Context.Response.Write(HttpContext.Current.Request.UrlReferrer.Host);
                HttpContext.Current.Response.WriteFile( HttpContext.Current.Server.MapPath("~/dl.jpg"));
                HttpContext.Current.Response.End();
            }
            //符合格式时,内部重写,地址栏不显示此地址
            Regex reg = new Regex(@".+MyStatic-(\d+).aspx");
            var match = reg.Match(HttpContext.Current.Request.Url.AbsolutePath);
            if(match.Success)
            {
                string page=match.Groups[1].Value;
                //仿静态页面
                HttpContext.Current.RewritePath("~/MyStatic.aspx?page="+page);
            }
           
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            //获取未处理的异常信息
            Exception t = HttpContext.Current.Server.GetLastError();
            File.AppendAllText(Server.MapPath("global.txt"), DateTime.Now.ToString() +t.Message+"服务器错误\r\n");
        }

        protected void Session_End(object sender, EventArgs e)
        {
            File.AppendAllText(Server.MapPath("global.txt"), DateTime.Now.ToString() + " ip:" + Request.UserHostAddress + " SessionEnd \r\n");
        }

        protected void Application_End(object sender, EventArgs e)
        {
            File.AppendAllText(Server.MapPath("global.txt"), DateTime.Now.ToString() + " AppEnd\r\n");
        }

 //第二种方法,建立类库(处理程序)重写url-处理任何请求

//类库handle里的类Myhandle

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace handle
{
    /// <summary>
    /// 处理请求,添加引用System.Web
    /// </summary>
    class Myhandle:IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.BeginRequest += MyRequest;
        }
        public void Dispose()
        {
           
        }
        protected void MyRequest(object sender, EventArgs e)
        {

System.Web.HttpApplication app = (System.Web.HttpApplication)sender;
            HttpContext context = app.Context;
            HttpResponse response = context.Response;

            string path = app.Context.Request.Path;

            // app.Context.Response.Write(path);
            string fname = System.IO.Path.GetFileName(path);
            if (fname.Contains(".html"))
            {
                string pathWithOutFilename = path.Replace(fname, "");
                System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("([A-Za-z1-9]{1,})\\-(\\d+).html");
                System.Text.RegularExpressions.Match match = reg.Match(fname);

                if (match.Success)
                {
                    //for (int i = 0; i < match.Groups.Count; i++)
                    //    app.Context.Response.Write(match.Groups[i].Value+"<br/>");
                    path = "~" + pathWithOutFilename + match.Groups[1].Value + ".aspx?id=" + match.Groups[2].Value;

                    ///http://localhost:6685/Read-14562.html
                    ///转换成实际的 http://localhost:6685/Read.aspx?id=14562
                    context.RewritePath(path);
                }
                else
                {
                    path = path.Replace(".do", ".aspx");
                    path = path.Replace(".html", ".aspx");
                    context.RewritePath("~" + path);
                }
            }

    }
      ///网站项目中添加对该类库的引用
      ///Web.config文件中加入注册处理程序
      //<httpModules>
      //  <add name="MyModule" type="handle.Myhandle,handle"/>  ///加入这一句,handle为类库项目名,Myhandle为类名
      //  <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      //</httpModules>
}

IIS设置html由asp_net处理。。

相关文章
|
15天前
|
存储 文字识别 C#
.NET开源免费、功能强大的 Windows 截图录屏神器
今天大姚给大家分享一款.NET开源免费(基于GPL3.0开源协议)、功能强大、简洁灵活的 Windows 截图、录屏、Gif动图制作神器:ShareX。
|
关系型数据库 MySQL
【Mysql】服务没有响应控制功能。 请键入 NET HELPMSG 2186 以获得更多的帮助。
解决方法: 1. 下载dll文件 https://www.aliyundrive.com/s/oV6GgghtPkN 2.将文件放置在mysql bin文件夹下 3. 重新启动Mysql,发现启动成功了!🚀
728 0
|
1月前
|
Windows
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
104 0
|
2月前
|
C# Windows
.NET开源的一个小而快并且功能强大的 Windows 动态桌面软件
.NET开源的一个小而快并且功能强大的 Windows 动态桌面软件
|
7月前
|
Apache
基于commons-net实现ftp创建文件夹、上传、下载功能.
基于commons-net实现ftp创建文件夹、上传、下载功能.
106 0
|
9月前
|
移动开发 监控 网络协议
基于Socket通讯(C#)和WebSocket协议(net)编写的两种聊天功能(文末附源码下载地址)
基于Socket通讯(C#)和WebSocket协议(net)编写的两种聊天功能(文末附源码下载地址)
|
5月前
|
开发框架 前端开发 .NET
用ajax和asp.net实现智能搜索功能
用ajax和asp.net实现智能搜索功能
43 0
|
9月前
|
SQL 安全 前端开发
.NET开源免费功能最全的商城项目
.NET开源免费功能最全的商城项目
|
9月前
|
开发框架 前端开发 JavaScript
WPF+ASP.NET SignalR实现简易在线聊天功能
WPF+ASP.NET SignalR实现简易在线聊天功能
129 0
|
9月前
|
数据库 C#
C#,.net,winform导入Excel功能以及下载Excel文件到本地,并使用SqlBulkCopy把DataTable类型的数据写入到sqlserver数据库中
C#,.net,winform导入Excel功能以及下载Excel文件到本地,并使用SqlBulkCopy把DataTable类型的数据写入到sqlserver数据库中
218 0