asp.net2.0 URL重寫以及urlMappings問題(1)_.Net教程

      編輯Tag賺U幣
      教程Tag:暫無Tag,歡迎添加,賺取U幣!

      推薦:談?wù)凥tmlControl與WebControl的區(qū)別與用途
      Web控件和Html控件雖然好多功能相同并且長得很像 但是它們的內(nèi)部實現(xiàn)機(jī)制是完全不一樣的 Web控件要比Html控件執(zhí)行效率要好 1. 使用起來也相當(dāng)方便,舉個簡單的例子,例如Button

      在asp.net2.0中的urlMappings倒是非常好用,可惜暫不支持正則表達(dá)式,不過,好在如果用IHttpModule的話
      不管什么樣的請求都會先經(jīng)過IHttpModule這樣就為URL重寫提供了一個好機(jī)會:

      下面是我寫的一個IHttpModule:

      using System;
      using System.Web;

      public class ReWriteModule : IHttpModule
      {
      public ReWriteModule()
      {
      }
      public override string ToString()
      {
      return this.GetType().ToString();
      }

      void IHttpModule.Dispose()
      {
      }
      private static System.Xml.XmlDocument ruleDoc = null;
      private static System.Xml.XmlDocument GetRuleConfig(System.Web.HttpContext app)
      {
      if (ruleDoc == null)
      {
      ruleDoc = new System.Xml.XmlDocument();
      ruleDoc.Load(app.Server.MapPath("~/rule.xml"));
      }
      return ruleDoc;
      }
      public static string GetUrl(System.Web.HttpContext cxt, string path)
      {
      System.Xml.XmlDocument doc = GetRuleConfig(cxt);
      System.Xml.XmlNodeList lst = doc.GetElementsByTagName("RewriterRule");
      string pat = "";
      foreach (System.Xml.XmlNode nd in lst)
      {
      System.Xml.XmlNodeList sub = nd.ChildNodes[0].ChildNodes;
      foreach (System.Xml.XmlNode chk in sub)
      {
      pat = "^" chk.InnerText "$";
      System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(pat, System.Text.RegularExpressions.RegexOptions.Compiled | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
      if (reg.IsMatch(path))
      {
      return reg.Replace(path, nd.ChildNodes[1].InnerText);
      }
      }
      }
      return null;
      }

      分享:ASP.NET 2.0服務(wù)器控件之客戶端功能
        多數(shù)在表示層應(yīng)用的服務(wù)器控件主要由兩個部分組成:服務(wù)器端功能和客戶端功能。服務(wù)器端功能永遠(yuǎn)是服務(wù)器控件的核心,而隨著技術(shù)的發(fā)展,客戶端功能也逐漸變得越來越重要。只有兩個部分

      共2頁上一頁12下一頁
      來源:模板無憂//所屬分類:.Net教程/更新時間:2008-08-22
      相關(guān).Net教程