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

首頁 > 學院 > 開發設計 > 正文

asp.net 1.1中url重寫的問題

2019-11-18 19:06:14
字體:
來源:轉載
供稿:網友

1:asp.net1.1中重寫中可刪節的問題!!!

如以下的正則表達式:

<Rules>
  <RewriterRule>
    <LookFors>
      <LookFor>~/(/d{4})/(/d{2})/.html</LookFor>---------<1>
      <LookFor>~/(/d{4})/(/d{2})/</LookFor>--------------<2>
      <LookFor>~/(/d{4})/(/d{2})</LookFor>-----------<3>
      <LookFor>~/(/d{4})/(/d{2})/index.html</LookFor>----<4>
    </LookFors>
    <SendTo>~/PRo.aspx?year=$1&amp;month=$2</SendTo>
  </RewriterRule>
 </Rules>

其中的1,4可以正常映射到對應的頁面

可2,3則會出現http404錯誤!!!

其原因在于IIS本身的處理流程,解決辦法則是在網站自己重寫404處理錯誤!!!

1:自定義處理404錯誤的URL(在IIS中配置,在web.config中的配置對重寫無用)

2:在System.Web節中添加如下節:

<httpHandlers>
   
          <add verb="*" path="404.aspx" type="lt.Http404,lt"></add>
   </httpHandlers>
   <httpModules>
  <add type="lt.ReWriteModule,lt" name="ModuleRewriter" />
 </httpModules>

源代碼如下:

  public class Http404:System.Web.IHttpHandler
 {
  public Http404()
  {
   //
   // TODO: 在此處添加構造函數邏輯
   //
  }
  #region IHttpHandler 成員

  public void ProcessRequest(System.Web.HttpContext context)
  {
   // TODO:  添加 Http404.ProcessRequest 實現
   string errorPath=context.Request.RawUrl.Split(new char[]{';'})[1];
   string appPath=context.Request.applicationPath;
   int ipos=errorPath.IndexOf(appPath);

   string  url=errorPath.Substring(ipos+appPath.Length );
//   if(!url.EndsWith("/"))
//   {
//      url+="/";
//   }
//   url+="index.html";
//   context.Response.Write(url);
//   context.RewritePath(url);

   //context.Response.Write(url);
      url="~"+url;
   string newUrl =lt.ReWriteModule.GetUrl(context,url);
   //context.Response.Write(newUrl);
   if (newUrl != null)
   {
    //cxt.Response.Filter = new ResponseFilter(cxt.Response.Filter,cxt.Request.Path);
    context.Response.Write("請求的路徑:" + url);
    context.Response.Write("<BR>");
    context.Response.Write("轉向的目的URL:" + newUrl);
    context.Response.Write("<BR>");
    context.RewritePath(newUrl);
   }
   else
   {
    context.Response.Write("你請求的資源不存在!!");
    context.Response.End ();
   }
  }

  public bool IsReusable
  {
   get
   {
    // TODO:  添加 Http404.IsReusable getter 實現
    return false;
   }
  }

///////////////配置節處理中的httpModule如下:

public class ReWriteModule:System.Web.IHttpModule
 {
  public ReWriteModule()
  {
   //
   // TODO: 在此處添加構造函數邏輯
   //
  }
  #region IHttpModule 成員

  public void Init(System.Web.HttpApplication context)
  {
   // TODO:  添加 ReWriteModule.Init 實現
   context.BeginRequest+=new EventHandler(this.ReWrite);
  }
  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;

  }
  private void ReWrite(object sender,EventArgs e)
  {
   System.Web.HttpContext cxt =(sender as System.Web.HttpApplication).Context;
    
   if (cxt.Request.ContentType != "image/pjpeg")
   {
    string type = cxt.Request.ContentType.ToLower();
    string path = cxt.Request.Path;
    string apppath = cxt.Request.ApplicationPath;
    path = path.Remove(0, apppath.Length);
    path = "~" + path;
           
    string newUrl = GetUrl(cxt, path.TrimEnd().TrimStart());
    if (newUrl != null)
    {
     //cxt.Response.Filter = new ResponseFilter(cxt.Response.Filter,cxt.Request.Path);
     cxt.Response.Write("請求的路徑:" + path);
     cxt.Response.Write("<BR>");
     cxt.Response.Write("轉向的目的URL:" + newUrl);
     cxt.Response.Write("<BR>");
     cxt.RewritePath(newUrl);
               
               
               
    }
    //else
    //{
    //    cxt.Response.Write(cxt.Request.Path + "<BR>");
    //    cxt.Response.Write("你請求的資源不存在或無權訪問!");
    //    cxt.Response.Flush();
    //    cxt.Response.End();
    //}
   }
  }
  public void Dispose()
  {
   // TODO:  添加 ReWriteModule.Dispose 實現
  }

  #endregion
 }

---------rule.xml的配置如下:

<?xml version="1.0" encoding="utf-8" ?>
<Rules>
  <RewriterRule>
    <LookFors>
      <LookFor>~/(/d{4})/(/d{2})/.html</LookFor>
      <LookFor>~/(/d{4})/(/d{2})/</LookFor>
      <LookFor>~/(/d{4})/(/d{2})</LookFor>
      <LookFor>~/(/d{4})/(/d{2})/index.html</LookFor>
    </LookFors>
    <SendTo>~/Pro.aspx?year=$1&amp;month=$2</SendTo>
  </RewriterRule>
  <RewriterRule>
    <LookFors>
      <LookFor>~/Pro.aspx?year=(/d{4})&amp;month=(/d{2})</LookFor>
    </LookFors>
    <SendTo>~/(/d{4})/(/d{2})/.html</SendTo>
  </RewriterRule>
  <RewriterRule>
    <LookFors>
      <LookFor>~/pc</LookFor>
    </LookFors>
    <SendTo>~/Test2.aspx</SendTo>
  </RewriterRule>
  <RewriterRule>
    <LookFors>
      <LookFor>~/index.html</LookFor>
      <LookFor>~/default.html</LookFor>
    </LookFors>
    <SendTo>~/default.aspx</SendTo>
  </RewriterRule>
</Rules>

/////////對于因重寫引起的action的改變,請參考本人寫的asp.net2.0中的urlMappings的問題!!!!!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 高碑店市| 通渭县| 山东省| 陇南市| 三台县| 伽师县| 宾阳县| 隆化县| 监利县| 临夏县| 乌鲁木齐县| 宁河县| 林西县| 义乌市| 顺平县| 镇安县| 越西县| 宁化县| 镶黄旗| 涞水县| 武穴市| 石家庄市| 连南| 漳平市| 祁连县| 万年县| 虞城县| 庆阳市| 五寨县| 尤溪县| 韶山市| 百色市| 玉溪市| 和硕县| 长宁县| 新邵县| 博客| 田林县| 西城区| 西安市| 靖安县|