国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

實現快速簡單高效并可以靈活配置的URL重寫方案

2024-07-21 02:29:15
字體:
來源:轉載
供稿:網友
,歡迎訪問網頁設計愛好者web開發。

需要源代碼請在評論里留下email

關鍵代碼: 
public class myhttpmodule : ihttpmodule
 {
  public void init(httpapplication app)
  {
   app.authorizerequest += new eventhandler(app_authorizerequest);
  }

  public void dispose() {}

  protected void rewrite(string requestedpath, system.web.httpapplication app)
  {
   //   app.context.rewritepath("~/default.aspx", string.empty, "test=tttttttt");
   foreach(urlrewrite url in siteurls.getsiteurls().urls)
   {
    if (regex.ismatch(app.context.request.path, url.pattern, regexoptions.compiled|regexoptions.ignorecase))
    {
     app.context.rewritepath(url.page, string.empty, regex.replace(app.context.request.path, url.pattern, url.querystring, regexoptions.compiled|regexoptions.ignorecase));
     return;
    }
   }
   if (app.context.request.path.tolower().endswith(".shtml"))
   {
    app.context.response.redirect("~/index.html");
   }
  }

  private void app_authorizerequest(object sender, eventargs e)
  {
   httpapplication app = (httpapplication) sender;
   rewrite(app.request.path, app);
  }
 }

 public class siteurls
 {
  #region 內部屬性和方法
  string siteurlsfile = httpcontext.current.server.mappath(configurationsettings.appsettings["siteurls"]);
  private arraylist _urls;
  public arraylist urls
  {
   get
   {
    return _urls;
   }
   set
   {
    _urls = value;
   }
  }

  private namevaluecollection _paths;
  public namevaluecollection paths
  {
   get
   {
    return _paths;
   }
   set
   {
    _paths = value;
   }
  }
  
  private siteurls()
  {
   string applicationpath = httpcontext.current.request.applicationpath;

   if (applicationpath == "/")
   {
    applicationpath = string.empty;
   }

   urls = new arraylist();
   paths = new namevaluecollection();
   paths.add("home", applicationpath);

   xmldocument xml = new xmldocument();

   xml.load(siteurlsfile);

   xmlnode root = xml.selectsinglenode("siteurls");
   foreach(xmlnode n in root.childnodes)
   {
    if (n.nodetype != xmlnodetype.comment && n.name.tolower() == "rewrite")
    {
     xmlattribute name = n.attributes["name"];
     xmlattribute path = n.attributes["path"];
     xmlattribute page = n.attributes["page"];
     xmlattribute querystring = n.attributes["querystring"];
     xmlattribute pattern = n.attributes["pattern"];

     if (name != null && path != null && page != null && querystring != null && pattern != null)
     {
      paths.add(name.value, applicationpath + path.value);
      urls.add(new urlrewrite(name.value, paths["home"]+pattern.value, paths["home"]+page.value.replace("^", "&"), querystring.value.replace("^", "&")));
     }
    }
   }
  }
  #endregion

  public static siteurls getsiteurls()
  {
   string cachekey = "siteurls";
   siteurls urls = system.web.httpcontext.current.cache["siteurls"] as siteurls;
   if (urls == null)
   {
    urls = new siteurls();
    system.web.httpcontext.current.cache.insert(cachekey, urls, new cachedependency(urls.siteurlsfile), datetime.maxvalue, timespan.zero, cacheitempriority.high, null);
   }

   return urls;
  }

  /// <summary>
  /// 輸出url示例
  /// </summary>
  /// <param name="id"></param>
  /// <returns></returns>
  public string show(int id)
  {
   return string.format(paths["show"], id);
  }

 public class urlrewrite
 {
  #region 成員變量
  private string _name;
  public string name
  {
   get
   {
    return _name;
   }
   set
   {
    _name = value;
   }
  }

  private string _pattern;
  public string pattern
  {
   get
   {
    return _pattern;
   }
   set
   {
    _pattern = value;
   }
  }

  private string _page;
  public string page
  {
   get
   {
    return _page;
   }
   set
   {
    _page = value;
   }
  }

  private string _querystring;
  public string querystring
  {
   get
   {
    return _querystring;
   }
   set
   {
    _querystring = value;
   }
  }
  #endregion

  #region 構造函數
  public urlrewrite(string name, string pattern, string page, string querystring)
  {
   _name = name;
   _pattern = pattern;
   _page = page;
   _querystring = querystring;
  }
  #endregion
 }

 public class pagebase : page
 {
  private pagebase()
  {
   //
   // todo: 在此處添加構造函數邏輯
   //
  }

  //// <summary>
  ///  重寫默認的htmltextwriter方法,修改form標記中的value屬性,使其值為重寫的url而不是真實url。
  /// </summary>
  /// <param name="writer"></param>
  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)
  {
   // 如果當前輸出的屬性為form標記的action屬性,則將其值替換為重寫后的虛假url
   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);
  }
 }

重寫配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<siteurls>
 <!--如果重寫shtml擴展名,需要在iis里,調整應用程序映射,把shtml擴展名映射到c:/windows/microsoft.net/framework/v1.1.4322/aspnet_isapi.dll,注意取消檢查文件是否存在-->
 <!--訪問方式http://localhost/example/show/323.shtml-->
    <rewrite name="show"
          path="/show/{0}.shtml"
          pattern = "/show/(/d+).shtml"
          page="/webform1.aspx"
          querystring="id=$1^cn=itemlist" />

</siteurls>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汨罗市| 夏津县| 宁乡县| 阿尔山市| 岑溪市| 景德镇市| 南召县| 海南省| 南郑县| 通化市| 肇州县| 绥化市| 读书| 牙克石市| 翁源县| 抚顺县| 金溪县| 绥棱县| 灵山县| 贡嘎县| 法库县| 阳原县| 甘泉县| 于田县| 射洪县| 台东市| 榆树市| 宝山区| 砚山县| 澄江县| 湖州市| 苍南县| 分宜县| 甘孜县| 金乡县| 饶河县| 望江县| 民勤县| 长阳| 广宗县| 永春县|