zoukankan      html  css  js  c++  java
  • 利用IHttpModule实现URL地址转发功能

    using System;
    using System.Web;
    using System.Text.RegularExpressions;

    namespace WebControlLibrary1
    {
        
    /// <summary>
        
    /// BaseModuleRewriter 的摘要说明。
        
    /// </summary>

        public abstract class BaseModuleRewriter:IHttpModule
        
    {
            
    public BaseModuleRewriter()
            
    {
                
    //
                
    // TODO: 在此处添加构造函数逻辑
                
    //
            }

            
    IHttpModule 成员

            
    protected virtual void app_AuthorizeRequest(object sender, EventArgs e)
            
    {
                HttpApplication app 
    = (HttpApplication)sender;
                
    this.Rewrite(app.Request.Path,app);
            }


            
    protected abstract void Rewrite(string requestedPath, HttpApplication app);

        }


        
    public class ModulRewriter:BaseModuleRewriter
        
    {
            
    protected override void Rewrite(string requestedPath, HttpApplication app)
            
    {
                
    string strPath = requestedPath;
                
    string strFileName = strPath.Substring(strPath.LastIndexOf("/")+1);
                
    string strReg = @"^\d+";
                Regex reg 
    = new Regex(strReg,RegexOptions.IgnoreCase);
                
    if(reg.IsMatch(strFileName))
                
    {
                    
                    
    string strTruePath = strPath.Remove(strPath.LastIndexOf("/")+1,strFileName.Length);
                    strTruePath 
    = strTruePath+"go.aspx?id=" + reg.Match(strFileName).Value;
                    HttpContext.Current.RewritePath(strTruePath);
                    
    //app.Server.Execute(strTruePath);
                }

                
    else
                
    {
                    
    //app.Server.Execute(strPath);
                    HttpContext.Current.RewritePath(strPath);
                }

            }


        }

    }


    其实网上已经有很多人都实现了,但我现在自己实现了一下,以加深印象。在web.config中写入:
     <httpModules>
      <add type="WebControlLibrary1.ModulRewriter, WebControlLibrary1" name="ModuleRewriter" />
     </httpModules>
    即可。
  • 相关阅读:
    Linux命令基础
    ubuntu16.04修改ssh的端口
    ubuntu16.04没有办法使用CRT,或者SSH工具的解决办法
    如何启动、关闭和设置ubuntu防火墙
    ubuntu远程桌面软件vnc。
    CAD安装激活失败的原因
    如何调出电脑的任务管理器
    如何查看Windows10连接的WiFi密码
    如何知道和你在一个局域网的电脑个数?
    如何快速连上另一台电脑已共享的打印机
  • 原文地址:https://www.cnblogs.com/sxlfybb/p/585138.html
Copyright © 2011-2022 走看看