实现IHttpHandler的类:
1using System;
2using System.Text.RegularExpressions;
3using System.Web;
4using System.Web.SessionState;
5
6namespace FaibClass.Common.Web
7{
8 public class URLRewriterHandler : IHttpHandler, IRequiresSessionState
9 {
10 public void ProcessRequest(HttpContext context)
11 {
12 URLRewriterRuleCollection rules = URLRewriterConfiguration.GetConfig().Rules;
13 string requestedPath = context.Request.FilePath;
14 for(int i = 0; i < rules.Count; i++)
15 {
16 Regex reg = new Regex(rules[i].MatchUrl, RegexOptions.IgnoreCase);
17 bool isMatch = reg.IsMatch(requestedPath);
18 if(isMatch)
19 {
20 context.Server.Execute(reg.Replace(requestedPath, rules[i].RedirectUrl));
21 }
22 }
23 }
24
25 public bool IsReusable
26 {
27 get
28 {
29 return true;
30 }
31 }
32 }
33}
2using System.Text.RegularExpressions;
3using System.Web;
4using System.Web.SessionState;
5
6namespace FaibClass.Common.Web
7{
8 public class URLRewriterHandler : IHttpHandler, IRequiresSessionState
9 {
10 public void ProcessRequest(HttpContext context)
11 {
12 URLRewriterRuleCollection rules = URLRewriterConfiguration.GetConfig().Rules;
13 string requestedPath = context.Request.FilePath;
14 for(int i = 0; i < rules.Count; i++)
15 {
16 Regex reg = new Regex(rules[i].MatchUrl, RegexOptions.IgnoreCase);
17 bool isMatch = reg.IsMatch(requestedPath);
18 if(isMatch)
19 {
20 context.Server.Execute(reg.Replace(requestedPath, rules[i].RedirectUrl));
21 }
22 }
23 }
24
25 public bool IsReusable
26 {
27 get
28 {
29 return true;
30 }
31 }
32 }
33}
使用IRequiresSessionState的目的是:能够在目标页面中使用Session。注意如果目标页面中使用Ajax.Net组件,在注册类型的时候应使用第二个参数:
AjaxPro.Utility.RegisterTypeForAjax(typeof(YourClass), this);
打开IIS管理器,在站点属性的映射中添加一个.fbsx的文件扩展名,其他设置与.aspx的一致,注意“确认文件是否存在”不能勾选。
在web.config中配置httpHandlers节:
1 <httpHandlers>
2 <add verb="POST,GET" path="*.fbsx" type="FaibClass.Common.Web.URLRewriterHandler, FaibClass.Common" />
3 </httpHandlers>
并配置重定向配置节2 <add verb="POST,GET" path="*.fbsx" type="FaibClass.Common.Web.URLRewriterHandler, FaibClass.Common" />
3 </httpHandlers>
<configSections>
<section name="URLRewriterConfig" type="FaibClass.Common.Web.URLRewriterConfigSerializerSectionHandler, FaibClass.Common" />
</configSections>
<URLRewriterConfig>
<Rules>
<URLRewriterRule>
<MatchUrl>([\d]+)\.fbsx</MatchUrl>
<RedirectUrl><![CDATA[target.aspx?to=$1]]></RedirectUrl>
</URLRewriterRule>
</Rules>
</URLRewriterConfig>
<section name="URLRewriterConfig" type="FaibClass.Common.Web.URLRewriterConfigSerializerSectionHandler, FaibClass.Common" />
</configSections>
<URLRewriterConfig>
<Rules>
<URLRewriterRule>
<MatchUrl>([\d]+)\.fbsx</MatchUrl>
<RedirectUrl><![CDATA[target.aspx?to=$1]]></RedirectUrl>
</URLRewriterRule>
</Rules>
</URLRewriterConfig>
这样,只要我们键入一个1234.fbsx文件,浏览器就会重定向到target.aspx页面进行相应的执行。