注:需要在该类库中添加system.web引用public class MyUrlWriter:IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler (context_BeginRequest);
}
protected void context_BeginRequest( object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication ;
HttpContext context = application.Context; //上下文
string url = context.Request.Url.LocalPath; //获得请求URL
Regex articleRegex = new Regex ("/index.html" ); //定义规则
if (articleRegex.IsMatch(url))
{
string paramStr = url.Substring(url.LastIndexOf('/' ) + 1);
context.RewritePath( "/Article.aspx?id=" + paramStr);
}
else
{
context.RewritePath( "/Default.aspx" );
}
}
public void Dispose() { }
}
Article.aspx代码如下:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
<a href ="/index.html">测试url重写</a>
</div>
</form>
</body>
</html>
三 在web.config的system.web的节点下添加如下代码:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Article.aspx.cs" Inherits="Article" %>
<!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>
<%=Request.QueryString["id"] %>
</div>
</form>
</body>
</html>
注:此处的type中的UrlReWriter对应上面命名的类库名称,MyUrlWriter对应上面类库中的类的命名。<httpModules>
<add name="UrlReWriter " type="UrlReWriter.MyUrlWriter,UrlReWriter" />
</httpModules>




名称 任意 比如test1,如图所示:

七 在iis中选中之前创建的网站,在右边的功能视图中双击模块-在右侧操作中点击添加托管模块,名称随意比如all,类型填写之前的类库名称.类名,勾选仅针对向asp.net或托管。。。,如下图所示:

八 将应用程序池中的托管管道模式由集成改为经典,在浏览器地址输入自己ip:端口号,点击链接,如出现404错误(注意红框位置):

如果处理程序为显示为StaticFile则表明前面的处理程序映射没有填写正确.net版本或者系统版本,如果是添加的映射程序名字,就把那个映射程序点开,点击请求限制,将映射中的仅当请求映射至以下内容时。。。。的勾选去掉。