在处理HttpHandler时要注意的几个问题

今天在网上找了一个处理URL重写的例子,原理使用Asp.Net中的HttpHandler实现URL重写的,当URL的后缀是XXX.shtml,而且XXX必须是数字的时候进行处理:

None.gif using  System;
None.gif
using  System.Web;
None.gif
using  System.Text.RegularExpressions;
None.gif
None.gif
namespace  test
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class NewHttpHandler:IHttpHandler
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public void ProcessRequest(HttpContext Context)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//申明Request
InBlock.gif
                HttpRequest Request = Context.Request;
InBlock.gif                
//取来路Url的绝对路径
InBlock.gif
                string Url = Request.Url.AbsolutePath;
InBlock.gif                
//取访问的Web文件的开始字符间隔数
InBlock.gif
                int RegStart = Url.LastIndexOf("/"+ 1;
InBlock.gif                
//申明一个确定Web文件名是否全是数字
InBlock.gif
                Regex Reg = new Regex(@"\d+");
InBlock.gif                
//用正则表达式进行匹配
InBlock.gif
                if (Reg.IsMatch(Url, RegStart))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//如果web文件名是数字,则判定是查询相关文章,执行指定页面
InBlock.gif
                    Context.Server.Execute("~/PermaLink.aspx?id=" + Reg.Match(Url, RegStart).Value);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Context.Response.Redirect(Context.Request.Url.ToString());
ExpandedSubBlockEnd.gif            }

InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取一个值,该值指示其他请求是否可以使用 IHttpHandler 实例。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public bool IsReusable 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedBlockEnd.gif}

然后是配置web.config的 <httpHandlers>节点:
None.gif       < httpHandlers >
None.gif        
< add verb = " * "  path = " *.shtml "  type = " test.NewHttpHandler,test "   />
None.gif    
</ httpHandlers >

接着配置IIS,刚刚开始的时候怎么也不能得到结果,郁闷……
最后发配置时的两个问题:
1.<httpHandlers>节点的写法:type="处理HttpHander的类的完全名称即命名空间+类名,程序集名称"
2.IIS中在Web项目的属性中,在主目录选项卡里,把执行权限改为"脚本和可执行文件",然后打开配置,*.shtml默认情况下处理程序是C:\WINDOWS\system32\inetsrv\ssinc.dll,如果要让ASP.net处理应改为C:\WINDOWS\system32\inetsrv\ssinc.dll,去掉检查文件是否存在的复选框
3.如果要改其他扩展名也是一样的道理,注意上面两个问题就是了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值