//第一种方法,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处理。。