加入收藏 | 设为首页 | 会员中心 | 我要投稿 焦作站长网 (https://www.0391zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > Asp教程 > 正文

在ASP.NET中用MSDNURLRewriting实现Url Rewriting

发布时间:2020-03-20 12:31:34 所属栏目:Asp教程 来源:互联网
导读:在ASP.NET中用MSDNURLRewriting实现Url Rewriting

注意:该BaseModuleRewriter类将网址重写放在AuthorizeRequest事件中调用,如果要使用Windows验证并使用文件验证模式时请修改代码将网址授权放在BeginRequest或者AuthenticateRequest事件中。
ModuleRewriter继承自BaseModuleRewriter,并真正意义地实现了网址重写的操作,该类仅包含一个重载了的方法Rewrite(),其内容如下文所示:

protected override void Rewrite(string requestedPath, System.Web.HttpApplication app)
{
    
// get the configuration rules
    RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;
    
// iterate through each rule
    for(int i = 0; i < rules.Count; i++)
    
{
        
// get the pattern to look for, and 
        
// Resolve the Url (convert ~ into the appropriate directory)
        string lookFor = "^" + 
        RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) 
+ "$";
        
// Create a regex (note that IgnoreCase is set)
        Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
        
// See if a match is found
        if (re.IsMatch(requestedPath))
        
{
            
// match found - do any replacement needed
            string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
            
// Rewrite the URL
            RewriterUtils.RewriteUrl(app.Context, sendToUrl);
            
break;      // exit the for loop
        }

    }

}

(编辑:焦作站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读