html5中文学习网

您的位置: 首页 > 网络编程 > ASP.NET » 正文

asp.net2.0 URL重写以及urlMappings问题(1)_.NET教程_编程技术

[ ] 已经帮助:人解决问题

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

    下面是我写的一个IHttpModule: 49QHTML5中文学习网 - HTML5先行者学习网

using System;
using System.Web;
49QHTML5中文学习网 - HTML5先行者学习网

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;
    }
49QHTML5中文学习网 - HTML5先行者学习网

共2页: 上一页 1 [2] 下一页 49QHTML5中文学习网 - HTML5先行者学习网

49QHTML5中文学习网 - HTML5先行者学习网
49QHTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助