• 自己正在用的url转义控件(带源码)


    忘记最初的源码怎么来的了,好像是微软的吧,后来觉得那个配置实在太烦琐了,用起来很是不方便。
    略微改了一下,初来报道,大家给个意见

    使用方法:
    1.
    web.config
    添加:
    <?xml version="1.0"?>
    <configuration>
        <system.web>
            <httpModules>
                <add name="ModuleRewriter" type="JIAOLG.URLRewriter.ModuleRewriter, UrlRewriter"/>
            </httpModules>
        </system.web>
    </configuration>

    2.
    把UrlRewriter.dll复制到bin目录

    3.
    把rewriter.xml复制到App_Data

    部分源码:
    using System;
    using System.Web;
    using System.Web.Caching;
    using System.Xml;
    using System.Text.RegularExpressions;

    namespace JIAOLG.URLRewriter
    {
        
    public class ModuleRewriter : BaseModuleRewriter
        {
            
    private static readonly string cname = "rules";
            
    private XmlNodeList rules;

            
    protected override void Rewrite(string requestedPath, System.Web.HttpApplication app)
            {
                app.Context.Trace.Write(
    "ModuleRewriter""Entering ModuleRewriter");

                
    if (HttpRuntime.Cache[cname] == null)
                {
                    CacheDependency cd 
    = new CacheDependency(Rules.ConfigFile);
                    
                    rules 
    = new Rules().getRules();
                    HttpRuntime.Cache.Insert(cname, rules, cd);
                }
                
    else
                {
                    rules 
    = (XmlNodeList)HttpRuntime.Cache[cname];
                }

                
    // iterate through each rule
                foreach (XmlNode rule in rules)
                {
                    
    string lf = rule.FirstChild.SelectSingleNode("lookfor").InnerText;
                    
    string st = rule.FirstChild.SelectSingleNode("sendto").InnerText;
                    
    string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, lf) + "$";

                    Regex re 
    = new Regex(lookFor, RegexOptions.IgnoreCase);

                    
    if (re.IsMatch(requestedPath))
                    {
                        
    string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, st));

                        app.Context.Trace.Write(
    "ModuleRewriter""Rewriting URL to " + sendToUrl);

                        RewriterUtils.RewriteUrl(app.Context, sendToUrl);
                        
    break;
                    }
                }

                app.Context.Trace.Write(
    "ModuleRewriter""Exiting ModuleRewriter");
            }
        }
    }

    下载(含源码):
    UrlRewriter.zip
  • 相关阅读:
    转载JGTM' 2004[MVP]有关AOP的三篇精彩文章
    新增Skin
    发表文章的要求
    自定义UserControl的属性为什么不能在设计时显示在属性窗口中
    .Text学习笔记(一)
    访问类的private或internal成员[转载]
    博客园对发表文章的一些要求
    博客园成立了管理团队
    推荐一篇介绍.NET MetaData的文章
    让大家久等了:终于完成了AOP尝鲜系列之第三部[JGTM'2004 [MVP]文章转载]
  • 原文地址:https://www.cnblogs.com/xiaozhai/p/828260.html
Copyright © 2020-2023  润新知