• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
梦想中的城市
让中国的程序员走向世界
博客园    首页    新随笔    联系   管理    订阅  订阅

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  夕阳夕下  阅读(591)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3