梦想中的城市
让中国的程序员走向世界
随笔- 16  文章- 0  评论- 90 
博客园  首页  新随笔  联系  管理  订阅 订阅

URL重写

今天在MSDN网站看了一些有关URL重写的文章,看了大半天,终于看完了,文字还真多啊。自己准备整理一下,备以后自己用,同时也希望给学习这方面的提供参考:
 第一步:新建项目
 第二步:添加引用
 1.引用URLRewriter.dll文件
第三步:新建ActionlessForm类,目的是为了处理URL回发
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace ActionlessForm
{
 /// <summary>
 /// The Form class extends the HtmlForm HTML control by overriding its RenderAttributes()
 /// method and NOT emitting an action attribute.
 /// </summary>
 public class Form : System.Web.UI.HtmlControls.HtmlForm
 {
  /// <summary>
  /// The RenderAttributes method adds the attributes to the rendered &lt;form&gt; tag.
  /// We override this method so that the action attribute is not emitted.
  /// </summary>
  protected override void RenderAttributes(HtmlTextWriter writer)
  {
   // write the form's name
   writer.WriteAttribute("name", this.Name);
   base.Attributes.Remove("name");

   // write the form's method
   writer.WriteAttribute("method", this.Method);
   base.Attributes.Remove("method");

   // remove the action attribute
   base.Attributes.Remove("action");

   // finally write all other attributes
   this.Attributes.Render(writer);

   if (base.ID != null)
    writer.WriteAttribute("id", base.ClientID);
  }

 }
}
在要处理回发的页面添加:<%@ Register TagPrefix="Skm" Namespace="ActionlessForm" Assembly="ActionlessForm"%> 并添加 <Skm:form id="Form1" runat="Server" action="test" >xxx</Skm:form>xxx代表任意服务器控件,如GridView
第四步:添加配置文件设置
如下:
 <configSections>
 <!-- The <configSections> element must contain a <section> tag for the <RewriterConfig> section element.
   The type of the section handler is RewriterConfigSerializerSectionHandler, which is responsible for
   deserializing the <RewriterConfig> section element into a RewriterConfig instance... -->
    <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
  </configSections>

注:LookFor:要查找的模式,SendTo:用来替换模式的字符串

<RewriterConfig>
 <Rules>
  <!-- Rules for Blog Content Displayer -->
  <RewriterRule>
   <LookFor>~/(\d{4})/(\d{2})/(\d{2})\.aspx</LookFor>
   <SendTo>~/ShowBlogContent.aspx?year=$1&amp;month=$2&amp;day=$3</SendTo>
  </RewriterRule>
  <RewriterRule>
   <LookFor>~/(\d{4})/(\d{2})/Default\.aspx</LookFor>
   <SendTo><![CDATA[~/ShowBlogContent.aspx?year=$1&month=$2]]></SendTo>
  </RewriterRule>
  <RewriterRule>
   <LookFor>~/(\d{4})/Default\.aspx</LookFor>
   <SendTo>~/ShowBlogContent.aspx?year=$1</SendTo>
  </RewriterRule>
  
 
  <!-- Rules for Product Lister -->
  <RewriterRule>
   <LookFor>~/Products/Default\.aspx</LookFor>
   <SendTo>~/ListCategories.aspx</SendTo>
  </RewriterRule>
</RewriterConfig>
到此为止结束,有不足之处,请大家见谅,具体请查看MSDN网站:http://msdn2.microsoft.com/zh-cn/library/ms972974.aspx

posted @ 2008-01-08 15:53 夕阳夕下 阅读(343) 评论(0) 编辑 收藏
刷新评论刷新页面返回顶部
程序员问答社区,解决您的IT难题
博客园首页博问新闻闪存程序员招聘知识库
Copyright ©2012 夕阳夕下