一、伪静态方式(使用URLRewriter)
1、改写方法 (文章地址:http://www.cnblogs.com/scottckt/archive/2011/01/12/1933737.html)
2、回发时处理方法(文章地址:http://www.cnblogs.com/scottckt/archive/2011/01/12/1933791.html)
3、将Aspx改写成HTml方法(文章地址:http://www.cnblogs.com/scottckt/archive/2011/01/12/1933836.html)
二、真实改写(使用System.Web.Routing)(文章地址:http://www.cnblogs.com/scottckt/archive/2011/01/12/1933893.html)
1、引用ActionlessForm.dll
然后,将 <form runat="server">(如果有)替换为: <skm:Form id="Form1" method="post" runat="server">,并将底部的 </form> 标记替换为: </skm:Form>
例如微软件提供测试项目RewriterTester中的ListProductsByCategory.aspx页面,此页面主要是为了处理分页功能。请看下边源码:
<%@ Page Trace="True" Language="c#" CodeBehind="ListProductsByCategory.aspx.cs" AutoEventWireup="false"
Inherits="RewriterTester.ListProductsByCategory" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Display Products</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1" />
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1" />
<meta name="vs_defaultClientScript" content="JavaScript" />
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5" />
</head>
<body>
<skm:Form ID="Form1" onsubmit="alert('foo');" action="test" runat="server">
<asp:Label ID="lblCategoryName" Font-Bold="True" Font-Size="18pt" runat="server"></asp:Label>
<asp:DataGrid ID="dgProducts" runat="server" AutoGenerateColumns="False" Width="369px"
Height="200px" BorderColor="White" BorderStyle="Ridge" CellSpacing="1" BorderWidth="2px"
BackColor="White" CellPadding="3" GridLines="None" AllowSorting="True">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#9471DE"></SelectedItemStyle>
<ItemStyle ForeColor="Black" BackColor="#DEDFDE"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#E7E7FF" BackColor="#4A3C8C"></HeaderStyle>
<FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="ProductName" HeaderText="Product Name" SortExpression="ProductName">
</asp:BoundColumn>
<asp:BoundColumn DataField="UnitPrice" HeaderText="Price" DataFormatString="{0:c}"
SortExpression="UnitPrice DESC">
<ItemStyle HorizontalAlign="Right"></ItemStyle>
</asp:BoundColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="Black" BackColor="#C6C3C6"></PagerStyle>
</asp:DataGrid>
</skm:Form>
</body>
</html>
此方法是继承page,这样你不需要在aspx页中改任何东西。个人并不推荐该方法。代码如下:
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);
}
}
}
改写为
public class WebForm1:URL.OLPage
参考文章:http://www.cnblogs.com/rickel/archive/2007/02/04/639616.html