查看: 7501|回复: 0
打印 上一主题 下一主题

asp.net 2.0中的URL重写以及urlMappings问题(2)

[复制链接]
跳转到指定楼层
1#
发表于 2007-9-16 11:55:35 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
台州网址导航
由于一旦进行了URL重写,原先的WEBFORM中的Action会发生改变,容易造成:请求的资源不存在问题
   
  具体怎么样?各位DX看看就清楚了!!!
   
  所有才有了这个ResponseFilter了,实现如下,
  1.   public class ResponseFilter:System.IO.Stream
  2.   {
  3.    public ResponseFilter(System.IO.Stream sink,string _str)
  4.    {
  5.    _sink = sink;
  6.    //
  7.    // TODO: 在此处添加构造函数逻辑
  8.    //
  9.    this.str = _str;
  10.    }
  11.    private string str = "";
  12.    private System.IO.Stream _sink;
  13.    private long _position;
  14.    private System.Text.Encoding end=System.Text.Encoding.GetEncoding("GB18030");
  15.    private System.Text.StringBuilder oOutput = new System.Text.StringBuilder();
  16.    // The following members of Stream must be overriden.
  17.    public override bool CanRead
  18.    {
  19.    get { return true; }
  20.    }
  21.    
  22.    public override bool CanSeek
  23.    {
  24.    get { return true; }
  25.    }
  26.    
  27.    public override bool CanWrite
  28.    {
  29.    get { return true; }
  30.    }
  31.    
  32.    public override long Length
  33.    {
  34.    get { return 0; }
  35.    }
  36.    
  37.    public override long Position
  38.    {
  39.    get { return _position; }
  40.    set { _position = value; }
  41.    }
  42.    
  43.    public override long Seek(long offset, System.IO.SeekOrigin direction)
  44.    {
  45.    return _sink.Seek(offset, direction);
  46.    }
  47.    
  48.    public override void SetLength(long length)
  49.    {
  50.    _sink.SetLength(length);
  51.    }
  52.    
  53.    public override void Close()
  54.    {
  55.    _sink.Close();
  56.    }
  57.    
  58.    public override void Flush()
  59.    {
  60.    _sink.Flush();
  61.    }
  62.    
  63.    public override int Read(byte[] buffer, int offset, int count)
  64.    {
  65.    return _sink.Read(buffer, offset, count);
  66.    }
  67.    
  68.    // The Write method actually does the filtering.
  69.    public override void Write(byte[] buffer, int offset, int count)
  70.    {
  71.    string szBuffer = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count);
  72.    string ap="action=\"";
  73.    int pos=-1;
  74.    if ((pos=szBuffer.IndexOf(ap) )!= -1)
  75.    {
  76.    int epos = szBuffer.IndexOf("\"", pos + ap.Length+1);
  77.    if (epos != -1)
  78.    {
  79.    szBuffer= szBuffer.Remove(pos + ap.Length, epos - pos - ap.Length);
  80.    }
  81.    
  82.    szBuffer = szBuffer.Insert(pos + ap.Length, this.str);
  83.    
  84.    byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(szBuffer);
  85.    _sink.Write(data, 0, data.Length);
  86.    
  87.    }
  88.    else
  89.    {
  90.    oOutput.Append(szBuffer);
  91.    }
  92.    
  93.    //下面的这一段可以用来修改之间的内容;
  94.    //Regex oEndFile = new Regex("", RegexOptions.IgnoreCase|RegexOptions.Compiled);
  95.    //if (oEndFile.IsMatch(szBuffer))
  96.    //{
  97.    // //Append the last buffer of data
  98.    // //附加上缓冲区中的最后一部分数据
  99.    // oOutput.Append(szBuffer);
  100.    // //Get back the complete response for the client
  101.    // //传回完整的客户端返回数据
  102.    // string szCompleteBuffer = oOutput.ToString().ToLower();
  103.    // int ipos = szCompleteBuffer.IndexOf("");
  104.    // int epos = szCompleteBuffer.IndexOf("",ipos+7);
  105.    // string sp = szCompleteBuffer.Substring(ipos+7, epos - ipos );
  106.    // szCompleteBuffer = szCompleteBuffer.Remove(ipos+7,sp.Length-7);
  107.    // szCompleteBuffer = szCompleteBuffer.Insert(ipos + 7, "dhz");
  108.    // // szCompleteBuffer = szCompleteBuffer.Replace(sp, "dhz");
  109.    // //No match, so write out original data
  110.    // //没有匹配,因此写入源代码
  111.    // byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(szCompleteBuffer);
  112.    // _sink.Write(data, 0, data.Length);
  113.    //}
  114.    //else
  115.    //{
  116.    // oOutput.Append(szBuffer);
  117.    //}
  118.    }
  119.   }
  120.    
  121.   //////而重候规则呢则是用xml文件配置如下;
  122.    
  123.   当然在web.config通过自定义配置节做也可以的
  124.    
  125.   <?xml version="1.0" encoding="utf-8" ?>
  126.    
  127.    
  128.    
  129.    ~/(\d{4})/(\d{2})\.html
  130.    ~/(\d{4})/(\d{2})/
  131.    ~/(\d{4})/(\d{2})
  132.    ~/(\d{4})/(\d{2})/index.html
  133.    
  134.    ~/Pro.aspx?year=$1&month=$2
  135.    
  136.    
  137.    
  138.    ~/pc
  139.    
  140.    ~/Test2.aspx
  141.    
  142.       
复制代码
//这个规则写的不好,如第一个就可以用一个正则表达式来做。但是一时不知道怎么写好,好像要用到什么反捕获组的概念,正在思考这个东东!!
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州朗动科技(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:政企网站,系统平台,微信公众号,各类小程序,手机APP客户端,浙里办微应用,浙政钉微应用、主机域名、虚拟空间、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2026 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表