伪URL

1.添加类URLRewriter

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for UrlRewriter
/// </summary>
public class UrlRewriter:IHttpHandler
{
 public UrlRewriter()
 {
  //
  // TODO: Add constructor logic here
  //
 }
    public void ProcessRequest(HttpContext Context)
    {
        try
        {
            //取得原始URL屏蔽掉参数
            string Url = Context.Request.RawUrl;
            //建立正则表达式
            System.Text.RegularExpressions.Regex Reg = new System.Text.RegularExpressions.Regex(@"/show-(\d+)-(\d+)\..+", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            //用正则表达式进行匹配
            System.Text.RegularExpressions.Match m = Reg.Match(Url,Url.LastIndexOf("/"));//从最后一个“/”开始匹配
            if (m.Success)//匹配成功
            {
                String RealPath = @"show.aspx?type=" + m.Groups[1] + "&id=" + m.Groups[2];
                Context.Server.Execute(RealPath);
            }
            else {
                Context.Response.Write( "404 ERROR!");
            }
        }
        catch
        {
            Context.Response.Redirect(Context.Request.Url.ToString());
        }
    }
    /// <summary>
    /// 实现“IHttpHandler”接口所必须的成员
    /// </summary>
    public bool IsReusable
    {
        get { return false; }
    }

}

2.在web.config文件<system.web>节点:

 <httpHandlers>
       <add verb="*" path="*/show-?*-?*.aspx" type="UrlRewriter" />
     </httpHandlers>

 

posted @ 2007-12-25 16:09  '.Elvis.'  阅读(207)  评论(0编辑  收藏  举报