自定义Url重写(支持ajax.net/atlas方案)

 1.定义url重写规则,很不幸的是一定要使用特定字符替代&,郁闷ing.........
< UrlReWriter  enabled ="true"  reverse ="true" >
  
< url  pattern ="/BBS/Forum/(d+).shtml"  url ="/BBS/Forum.aspx?BoardID=$1"  repattern ="/BBS/Forum.aspx?BoardID=(d+)$"  reurl ="/BBS/Forum/$1.shtml"   />
  
< url  pattern ="/BBS/Post/(d+).shtml"  url ="/BBS/Post.aspx?ArticleID=$1"  repattern ="/BBS/Post.aspx?ArticleID=(d+)$"  reurl ="/BBS/Post/$1.shtml" ></ url >
  
< url  pattern ="/BBS/Forum/(d+)/(d+).shtml$"  url ="/BBS/Forum.aspx?BoardID=$1^type=$2"  repattern ="/BBS/Forum.aspx?BoardID=(d+)^type=(d+)$"  reurl ="/BBS/Forum/$1/$2.shtml"   />
</ UrlReWriter >

2.定义配置文件对应实体类

 

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Web;
using  System.Xml;
using  System.Web.Caching;

namespace  DuwCompontents
{
    
/// <summary>
    
/// url重写配置节实体类
    
/// </summary>

    public class UrlReWriter
    
{
        
...

        
...

        
构造

    }

    
/// <summary>
    
/// url重写配置实体类
    
/// </summary>

    public class UrlReWriterConfig
    
{

        
私有成员

        
公有属性

        
单件模式
       
    }

}

3.实现IHttpModule(不使用ajax.net(atlas)可以不用处理AjaxPageFilter相关)

 

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Web;
using  System.Web.Security;
using  System.Xml;
using  System.Text.RegularExpressions;

namespace  DuwCompontents
{
    
public interface IRawPath
    
{
        
string GetPath();
    }

    
public class DuwHttpModule : IHttpModule, IRawPath
    
{

        
IRawPath 成员
        
IHttpModule 成员

        
事件

    }

}

4.配置IIS,主目录/配置,使用aspnet_isapi.dll处理.shtml.取消确认文件是否存在选择项

5,配置web.Config.支持自定义httpModule

 

< System.Web >
    
< httpModules >
        
< add name = " ScriptModule "  type = " System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 " />
        
<!-- type = " 完全限定名,程序集 " -->
        
< add name = " DuwHttpModule "  type = " DuwCompontents.DuwHttpModule,DuwCompontents " />
    
</ httpModules >
</ System.Web >

6重写page类
using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  DuwCompontents;
using  System.Text.RegularExpressions;
using  System.IO;
using  System.Web;

namespace  DuwControls
{
    
public class DuwPage : Page
    
{


       
        
事件

        
替换所有的服务端超链接

        
替换掉Form的action
    }

}

7继续使用页面过滤器,使支持ajax.net(atlas)

 

using  System;
using  System.Text;
using  System.Text.RegularExpressions;
using  System.IO;
using  System.Web;

namespace  DuwCompontents
{
    
public class AjaxPageFilter : Stream
    
{
        IRawPath _context 
= null;
        Stream responseStream;
        
long position;
        StringBuilder responseHtml;

        
public AjaxPageFilter(Stream inputStream)
        
{
            responseStream 
= inputStream;
            responseHtml 
= new StringBuilder();
        }


        
public override bool CanRead
        
{
            
get return true; }
        }


        
public override bool CanSeek
        
{
            
get return true; }
        }


        
public override bool CanWrite
        
{
            
get return true; }
        }


        
public override void Close()
        
{
            responseStream.Close();
        }


        
public override void Flush()
        
{
            responseStream.Flush();
        }


        
public override long Length
        
{
            
get return 0; }
        }


        
public override long Position
        
{
            
get return position; }
            
set { position = value; }
        }


        
public override long Seek(long offset, SeekOrigin origin)
        
{
            
return responseStream.Seek(offset, origin);
        }


        
public override void SetLength(long length)
        
{
            responseStream.SetLength(length);
        }


        
public override int Read(byte[] buffer, int offset, int count)
        
{
            
return responseStream.Read(buffer, offset, count);
        }


        
public override void Write(byte[] buffer, int offset, int count)
        
{
            
string finalHtml = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count);

            
// Transform the response
            string formAction = RawPath.GetPath();
            
if (!String.IsNullOrEmpty(formAction))
            
{
                finalHtml 
= Regex.Replace(finalHtml, "/|/d+/|formAction/|/|[^/|]+/|",
                    
"|" + formAction.Length.ToString() + "|formAction||" + formAction + "|",
                    RegexOptions.IgnoreCase);
            }


            
// Write the response
            byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(finalHtml);
            responseStream.Write(data, 
0, data.Length);
        }


        
public IRawPath RawPath
        
{
            
get
            
{
                
return _context;
            }

            
set
            
{
                _context 
= value;

            }

        }

    }

}

8.取消3(DuwHttpModule)中的注释,转而支持AjaxPageFilter,
至此url重写工作完成,但是反向匹配虽然可以使我们几乎不用改任何一句代码
就支持全站的url重写,却是一个很浪费资源的行为,并不建议使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值