URL 重写就是把URL地址重新改写

本文介绍了URL重写的基本概念及在ASP.NET中的具体实现方法,包括如何通过配置重定向规则来实现URL的美化和缩短,同时探讨了解决URL重写后页面回发导致地址栏变化的方法。
            作者:overred   来源:原创
URL 重写就是把URL地址重新改写(汗^_^)。
详情: http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx
优点:把url缩短等
用法:1.下载ms的URLRewrite.dll,放到你的bin下
2.在web.config里设置如下:
None.gif
None.gif
<? xml version="1.0" encoding="utf-8"  ?>
None.gif
< configuration >
None.gif
< configSections >
None.gif    
< section  name ="RewriterConfig"  type ="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"   />
None.gif
</ configSections >
None.gif
< RewriterConfig >
None.gif
< Rules >
None.gif
< RewriterRule >
None.gif
< LookFor > ~/d(\d+)\.aspx </ LookFor >
None.gif
< SendTo > ~/default.aspx?id=$1 </ SendTo >
None.gif
</ RewriterRule >
None.gif
</ Rules >
None.gif
</ RewriterConfig >
None.gif
< system .web >
None.gif
< httpHandlers >
None.gif
< add  verb ="*"  path ="*.aspx"  
None.giftype
="URLRewriter.RewriterFactoryHandler, URLRewriter"   />
None.gif
</ httpHandlers >
然后在cs里写:
1 None.gif private   void  Page_Load( object  sender, System.EventArgs e)
2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
3InBlock.gif// 在此处放置用户代码以初始化页面
4InBlock.gifResponse.Write(Request.QueryString["id"]+"<BR>");
5InBlock.gifResponse.Write("haha");
6ExpandedBlockEnd.gif}

7 None.gif

只要输入

localhost/overred/d123.aspx(注意:开头必须为d,后为数字)
其实这个d123.aspx是虚拟的,并不是实际存在的。只要符合格式就行。

他就会跳到http://localhost/overred/default.aspx

而且他在default里可以捕捉一些参数比如id,就是你的d后的数字(后必须为数字),这样你就可以显示id为123的文章。

在重写后的url里如果产生回发将会传递到d123.aspx,这样用户在点button时会看到哪个实际的地址,msdn上说的:但从用户的角度考虑,如果单击按钮时突然看到 URL 更改会使他们感到不安。

可见ms把客户捧为他的上帝!(真的?#¥%……—*)

继续引用ms:

出现这种情况的原因是:在呈现 Web 窗体时,它会将其操作属性直接设置为 Request 对象中文件路径的值。当然,在呈现 Web 窗体时,URL 已从 /Products/Beverages.aspx 重写为 ListProductsByCategory.aspx?CategoryID=1,这表明 Request 对象报告用户要访问 ListProductsByCategory.aspx?CategoryID=1。只需使服务器端窗体不呈现操作属性即可解决此问题。(默认情况下,如果窗体不包含操作属性,浏览器将会回发。)

不幸的是,Web 窗体不允许您明确指定操作属性,也不允许您设置某些属性以禁用操作属性的呈现。因此,我们必须自己来扩展 System.Web.HtmlControls.HtmlForm 类,覆盖 RenderAttribute() 方法并明确指出它不会呈现操作属性。

由于继承功能,我们可以获得 HtmlForm 类的所有功能,并且只需添加几行代码即可获得所需的行为。以下显示了自定义类的完整代码:

ExpandedBlockStart.gif ContractedBlock.gif namespace  ActionlessForm  dot.gif {
InBlock.gif
public class Form : System.Web.UI.HtmlControls.HtmlForm
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
protected override void RenderAttributes(HtmlTextWriter writer)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifwriter.WriteAttribute(
"name"this.Name);
InBlock.gif
base.Attributes.Remove("name");
InBlock.gif
InBlock.gifwriter.WriteAttribute(
"method"this.Method);
InBlock.gif
base.Attributes.Remove("method");
InBlock.gif
InBlock.gif
this.Attributes.Render(writer);
InBlock.gif
InBlock.gif
base.Attributes.Remove("action");
InBlock.gif
InBlock.gif
if (base.ID != null)
InBlock.gifwriter.WriteAttribute(
"id"base.ClientID);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif

已被覆盖的 RenderAttributes() 方法的代码仅包含 HtmlForm 类的 RenderAttributes() 方法的准确代码,而不设置操作属性。(我使用 Lutz Roeder 的 Reflector 来查看 HtmlForm 类的源代码。)

创建此类并对其进行编译之后,要在 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>

以上的是继承个form,其实还有更简单的,就是继承page,这样你不需要在aspx页中改任何东西。

None.gif using  System;
None.gif
using  System.IO;
None.gif
using  System.Web;
None.gif
using  System.Web.UI;
None.gif
None.gif
namespace  URl
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//**//**//// <summary>
InBlock.gif
/// 页面基类
ExpandedSubBlockEnd.gif
/// </summary>

InBlock.gifpublic class OLPage : Page
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
public OLPage()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//**//**//// <summary>
InBlock.gif
/// 重写默认的HtmlTextWriter方法,修改form标记中的value属性,使其值为重写的URL而不是真实URL。
InBlock.gif
/// </summary>
ExpandedSubBlockEnd.gif
/// <param name="writer"></param>

InBlock.gifprotected override void Render(HtmlTextWriter writer)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gif
if (writer is System.Web.UI.Html32TextWriter)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifwriter 
= new FormFixerHtml32TextWriter(writer.InnerWriter);
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifwriter 
= new FormFixerHtmlTextWriter(writer.InnerWriter);
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
base.Render(writer);
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
internal class FormFixerHtml32TextWriter : System.Web.UI.Html32TextWriter
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
private string _url; // 假的URL
InBlock.gif

InBlock.gif
internal FormFixerHtml32TextWriter(TextWriter writer):base(writer)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif_url 
= HttpContext.Current.Request.RawUrl;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
public override void WriteAttribute(string name, string value, bool encode)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
// 如果当前输出的属性为form标记的action属性,则将其值替换为重写后的虚假URL
InBlock.gif
if (_url != null && string.Compare(name, "action"true== 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifvalue 
= _url;
ExpandedSubBlockEnd.gif}

InBlock.gif
base.WriteAttribute(name, value, encode);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
internal class FormFixerHtmlTextWriter : System.Web.UI.HtmlTextWriter
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
private string _url;
InBlock.gif
internal FormFixerHtmlTextWriter(TextWriter writer):base(writer)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif_url 
= HttpContext.Current.Request.RawUrl;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
public override void WriteAttribute(string name, string value, bool encode)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if (_url != null && string.Compare(name, "action"true== 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifvalue 
= _url;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
base.WriteAttribute(name, value, encode);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedBlockEnd.gif}

None.gif


你把他封装成dll,以后只要添加引用就可以拉!

 

ok ,it is so easy!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值