URL重写之实现IHttpHandler接口 .Net 1.1实现

 在幻想曲的帮助下,在这个接口基础上,我改了少少,用asp.net 1.1版实现,与大家分享
代码如下:

1.新建工程SofteerStudio,代码如下:

using  System; 
using  System.IO; 
using  System.Data; 
using  System.Configuration; 
using  System.Collections;
using  System.Web; 
using  System.Web.Security; 
using  System.Web.UI; 
using  System.Web.UI.WebControls; 
using  System.Web.UI.HtmlControls; 
using  System.Text; 
using  System.Text.RegularExpressions; 
using  Microsoft.VisualBasic; 

namespace  SofteerStudio
{

    
//RegexInfo结构,用来存储从xml文件中读取到的数据 
    public struct RegexInfo 
    

        
public string _before; 
        
public string _after; 
        
public RegexInfo(string before, string after) 
        

            _before 
= before.ToLower(); 
            _after 
= after.ToLower(); 
        }
 
    }
 
    
//ipFilter结构,用来存储被封的IP 
    public struct ipFilter 
    

        
public string _ip; 
        
public ipFilter(string ip) 
        

            _ip 
= ip; 
        }
 
    }
 


    
/// <summary>
    
/// HtmlHttpHandler 的摘要说明。
    
/// </summary>

    public class HtmlHttpHandler: IHttpHandler
    
{
        
        
private IList _regex_list=new ArrayList() ; 
        
private IList _ip_filter=new ArrayList(); 

        
public HtmlHttpHandler() 
        

            DataSet ds 
= new DataSet(); 
            
//读取url重写规则文件,并写入RegexInfo结构的实例中 
            ds.ReadXml(System.Web.HttpContext.Current.Server.MapPath("~/XMLConfig/Config/Regexs.xml")); 

            
foreach (DataRow r in ds.Tables["regex"].Rows) 
                
                _regex_list.Add(
new RegexInfo(((string)r["b"]).Trim(), ((string)r["a"]).Trim())); 
            ds.Reset(); 
            
//读取被封的IP列表 
            ds.ReadXml(System.Web.HttpContext.Current.Server.MapPath("~/XMLConfig/Config/ipFilter.xml")); 
            
foreach(DataRow r in ds.Tables["IpFilters"].Rows) 
                _ip_filter.Add(
new ipFilter((string)r["ip"])); 
        }
 

        
public void ProcessRequest(HttpContext context) 
        

            
string _ip = context.Request.UserHostAddress;   //获取IP 
            foreach (ipFilter r in _ip_filter) 
            

                
if (_ip == r._ip) 
                

                    context.Response.Write(
"对不起,您的IP:"+_ip+"已被限制!"); 
                    context.Response.End(); 
                }
 
            }
 
            
string path = context.Request.Path.ToLower();   //获取当前访问的重写过的虚假URL 
            foreach (RegexInfo r in _regex_list) 
                path 
= Regex.Replace(path, r._before, r._after);      //匹配出其真实的URL 
            context.Server.Execute(path);  
        }
 

        
// Override the IsReusable property. 
        public bool IsReusable 
        

            
get return true; } 
        }
 
    }

}

 


2.编译成dll,引用它至你的项目中.

3.更改你的项目web.config

  < httpHandlers >  
      
< add verb = " * "  path = " *.html "  type = " SofteerStudio.HtmlHttpHandler,SofteerStudio " />  
 
</ httpHandlers >  

 

 

欢迎与大家交流,我的gmail是softeer#gmail.com

 

附:xml文件的格式:

 

<? xml version="1.0" encoding="utf-8"  ?>  
< root >  
< regex >  
                
<!-- 重写以后的虚拟地址 -->  
< b > <![CDATA[ xxx,(?<id>[0-9]+).html$ ]]> </ b >  
<!-- 实际地址 -->  
                
< a > <![CDATA[ xxx.aspx?id=${id} ]]> </ a >          
</ regex >  
</ root >  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET 是一种用于构建 Web 应用程序的开发框架,它提供了丰富的功能和工具来简化开发过程。要在 ASP.NET实现 WebSocket,可以按照以下步骤操作: 1. 在项目中引入 System.Net.WebSockets 命名空间。 2. 在 Global.asax 文件中,注册 WebSocket 模块。可以在 Application_Start 方法中添加以下代码: ```csharp void Application_Start(object sender, EventArgs e) { // 注册 WebSocket 模块 System.Web.Routing.RouteTable.Routes.MapWebSocketRoute("websocket", "/websocket"); } ``` 3. 创建一个实现 IHttpHandler 接口的类来处理 WebSocket 请求。可以创建一个名为 WebSocketHandler 的类,并实现 ProcessRequest 方法: ```csharp public class WebSocketHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { if (context.IsWebSocketRequest) { context.AcceptWebSocketRequest(HandleWebSocket); } } private async Task HandleWebSocket(AspNetWebSocketContext context) { var socket = context.WebSocket; // 处理 WebSocket 连接 // 可以使用 socket.ReceiveAsync 和 socket.SendAsync 方法进行消息的接收和发送 } public bool IsReusable => false; } ``` 4. 在 web.config 文件中配置路由处理程序。在 `<system.webServer>` 节点内添加以下代码: ```xml <handlers> <add name="WebSocketHandler" path="/websocket" verb="*" type="WebSocketHandler" preCondition="integratedMode" /> </handlers> ``` 这样就完成了 ASP.NET 中 WebSocket 的实现。你可以在 WebSocketHandler 类的 HandleWebSocket 方法中处理 WebSocket 连接,并使用 socket.ReceiveAsync 和 socket.SendAsync 方法进行消息的接收和发送。记得根据自己的实际需求进行处理和逻辑编写。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值