处理回发
系列文章目录
一、伪静态方式(使用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)
在重写后的url里如果产生回发,页面就会显示实际地址。如http://hostname/2003/Default地址就会显示成http://hostname/ShowBlogContent.aspx?year=2003。
解决方法一——使用微软提供的ActionlessForm.dll
1、引用ActionlessForm.dll
在UrlRewriter方案中有ActionlessForm项目,编译后生成ActionlessForm.dll。在改写URL的项目中引用ActionlessForm.dll。
2、使用ActionlessForm替换Form
先在 ASP.NET 网页的顶部添加以下内容:<%@ Register TagPrefix="skm" Namespace="ActionlessForm" Assembly="ActionlessForm" %>
然后,将 <form runat="server">(如果有)替换为: <skm:Form id="Form1" method="post" runat="server">,并将底部的 </form> 标记替换为: </skm:Form>
例如微软件提供测试项目RewriterTester中的ListProductsByCategory.aspx页面,此页面主要是为了处理分页功能。请看下边源码:
<%@ Register TagPrefix="skm" Namespace="ActionlessForm" Assembly="ActionlessForm" %>
<
%@ 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;
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
参考文章:http://www.cnblogs.com/rickel/archive/2007/02/04/639616.html