当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > asp.net2.0 URL重写以及urlMappings问题(1)

ASP.NET
asp.net服务器上几种常见异常的解决方案.
Asp.net 下载功能的解决方案
asp.net 页面传值的几个方法
asp.net Cookie跨域、虚拟目录等设置方法
aspnet_isapi.dll设置图文方法.net程序实现伪静态
ASP.NET Web应用程序的安全解决方案浅析
asp.net 图片的读写入库实现代码
asp.net cookie的读写实例
浅析ASP.NET生成随机密码函数
asp.net 防止用户通过后退按钮重复提交表单
ASP.NET 调用百度搜索引擎的代码
asp.net用url重写URLReWriter实现任意二级域名 新
asp.net用url重写URLReWriter实现任意二级域名 高级篇
asp.net 下载文件时根据MIME类型自动判断保存文件的扩展名
asp.net 文件上传 实时进度
asp.net+jquery Gridview的多行拖放, 以及跨控件拖放
ASP.NET 页面之间传递值方式优缺点比较
asp.net 页面转向 Response.Redirect, Server.Transfer, Server.Execute的区别
ASP.NET 返回随机数实现代码
asp.net FreeTextBox配置详解

ASP.NET 中的 asp.net2.0 URL重写以及urlMappings问题(1)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 106 ::
收藏到网摘: n/a

    在asp.net2.0中的urlMappings倒是非常好用,可惜暂不支持正则表达式,不过,好在如果用IHttpModule的话
不管什么样的请求都会先经过IHttpModule这样就为URL重写提供了一个好机会:

    下面是我写的一个IHttpModule:

using System;
using System.Web;

public class ReWriteModule : IHttpModule
{
    public ReWriteModule()
    {
    }
    public override string ToString()
    {
        return this.GetType().ToString();
    }

    void IHttpModule.Dispose()
    {
    }
    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;
    }

    void IHttpModule.Init(HttpApplication context)
    {
        context.BeginRequest += delegate(object sender, EventArgs e)
        {
            System.Web.HttpContext cxt = context.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("");
                    cxt.Response.Write("转向的目的URL:" + newUrl);
                    cxt.Response.Write("");
                    cxt.RewritePath(newUrl);
                }
  //如果要求处理所有的请求时用到
                //else
                //{
                // cxt.Response.Write(cxt.Request.Path + "");
                // cxt.Response.Write("你请求的资源不存在或无权访问!");
                // cxt.Response.Flush();
                // cxt.Response.End();
                //}
            }
        };
    }
}