1. 下载ms的URLRewriter.dll
下载地址:
http://download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi
2.新建Web,将URLRewriter.dll引用到项目中。
3.修改配置文件,具体的配置内容如下:

WebConfig
<?xml version="1.0"?>
<!--
Note: As an alternative to hand editing this file you can use the
web admin tool to configure settings for your application. Use
the Website->Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in
machine.config.comments usually located in
\Windows\Microsoft.Net\Framework\v2.x\Config
-->
<configuration>
<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>
</configSections>
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>~/d(\d+)\.aspx</LookFor>
<SendTo>~/default.aspx?id=$1</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/d(\d+)/.aspx</LookFor>
<SendTo>~/default.aspx?id=$1</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
<system.web>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="true"/>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows"/>
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<!--
如果在执行请求的过程中出现未处理的错误,
则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
开发人员通过该节可以配置
要显示的 html 错误页
以代替错误堆栈跟踪。

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<httpHandlers>
<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter"/>
</httpHandlers>
</system.web>
</configuration>
其中
<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections>
用于指定配置节"RewriterConfig"的处理程序类的名称为”URLRewriter.Config.RewriterConfigSerializerSectionHandler”,该类存在于bin目录下的URLRewriter .dll文件中
在configuration节点下加入如下代码:
配置节"RewriterConfig"内容如下
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>~/d(\d+)\.aspx</LookFor>
<SendTo>~/default.aspx?id=$1</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
其中关键的是这两句
<LookFor>~/d(\d+)\.aspx</LookFor>
<SendTo>~/default.aspx?id=$1</SendTo>
<LookFor>~/d(\d+)\.aspx</LookFor>表示,用户输入的url,d(\d+)\.aspx是 url中文件名匹配的正则表达式(此处为字母d开头,后面跟一个或多个数字,并以.aspx结尾。用户也可根据自己的需要自行设定)。
<SendTo> ~/default.aspx?id=$1</SendTo>,表示当服务器接收到符合上面条件的请求后如何重写url。此处表示访问 defalutl.aspx并传入参数id,其值$1将用用户请求的文件名中的第一个数字来表示。
例如用户输入 hostname/d11.aspx,服务器会把他重写为http://hostname/default.aspx?id=11。换句话说用户输入http: //hostname/d11.aspx,实际访问的是http://hostname/default.aspx?id=11。这样就起到了隐藏真实文件名,并便于用户记忆的作用。
3.处理回发
在重写后的url里如果产生回发,例如有一个按钮,又调用了该被重写的aspx,用户浏览器中将会显示该aspx文件实际的地址,也就是http: //hostname/default.aspx?id=11。但从用户的角度考虑,如 果单击按钮时突然看到 URL 更改会使他们感到不安。因此必须解决这个问题。
解决方法有二:
(1)自己定义一个Actionlessform类,在aspx中不再使用系统提供的form 标记
创建此类并对其进行编译之后,要在 ASP.NET Web 应用程序中使用它,应首先将其添加到 Web 应用程序的 References 文件夹中。然后,要 使用它来代替 HtmlForm 类,做法是在 ASP.NET 网页的顶部添加以下内容:
<%@ Register TagPrefix="skm" Namespace="ActionlessForm"
Assembly="ActionlessForm" %>
然后,将 <form runat="server">(如果有)替换为:
<skm:Form id="Form1" method="post" runat="server">
并将右边的 </form> 标记替换为:
</skm:Form>

Code
using System.Text;
using System.IO;
using System.Web;
using System.Web.UI;

namespace ActionlessForm


{
public class Form : System.Web.UI.HtmlControls.HtmlForm

{
protected override void RenderAttributes(HtmlTextWriter writer)

{
writer.WriteAttribute("name", this.Name);
base.Attributes.Remove("name");
writer.WriteAttribute("method", this.Method);
base.Attributes.Remove("method");
this.Attributes.Render(writer);
base.Attributes.Remove("action");
if (base.ID != null)
writer.WriteAttribute("id", base.ClientID);
}
}
}
在页面中实现:

Code

<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>


<%
@ Register TagPrefix="skm" Namespace="ActionlessForm" Assembly="ActionlessForm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<skm:Form id="Form1" method="post" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" /><%=Request.QueryString["id"] %>
</div>
</skm:Form>
</body>
</html>
(2)以上的是继承个form,第二种方法就是继承page,这样你不需要在aspx页中改任何东西。

Code
using System;
using System.IO;
using System.Web;
using System.Web.UI;

namespace URL


{
public class OLPage : Page

{
public OLPage()

{ }
protected override void Render(HtmlTextWriter writer)

{
if (writer is System.Web.UI.Html32TextWriter)

{
writer = new FormFixerHtml32TextWriter(writer.InnerWriter);
}
else

{
writer = new FormFixerHtmlTextWriter(writer.InnerWriter);
}
base.Render(writer);
}
}

internal class FormFixerHtml32TextWriter : System.Web.UI.Html32TextWriter

{
private string _url; // 假的URL
internal FormFixerHtml32TextWriter(TextWriter writer)
: base(writer)

{
_url = HttpContext.Current.Request.RawUrl;
}
public override void WriteAttribute(string name, string value, bool encode)

{
if (_url != null && string.Compare(name, "action", true) == 0)

{
value = _url;
}
base.WriteAttribute(name, value, encode);
}
}
internal class FormFixerHtmlTextWriter : System.Web.UI.HtmlTextWriter

{
private string _url;
internal FormFixerHtmlTextWriter(TextWriter writer)
: base(writer)

{
_url = HttpContext.Current.Request.RawUrl;
}
public override void WriteAttribute(string name, string value, bool encode)

{
if (_url != null && string.Compare(name, "action", true) == 0)

{
value = _url;
}
base.WriteAttribute(name, value, encode);
}
}
}

把这个文件编译成dll,并在你的项目中引用它。
然后把项目中的所有aspx文件对应的cs文件中的继承page类的代码改写为继承OLPage。
例如
public class WebForm1:page
改写为
public class WebForm1:URL.OLPage
这样就解决回发问题。
整个实例代码:整个源程序