论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > Asp.net教程
Tag:静态页面,treeview,gridview,repeater,dataset,sqldatareader,ado.net,上传,三层,ajax,xml,留言本,新闻发布,商城,注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,触发器,socket,form认证,登录,视频教程

asp.net 1.1中url重写的问题

文章类别:Asp.net | 发表日期:2008-10-5 22:12:17

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的问题!!!!!

视频教程列表
文章教程搜索
 
Asp.net推荐教程
Asp.net热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058