Asp.net URL 重写

http://www.cnblogs.com/fengfeng/archive/2008/02/15/1069462.html

using System;
using System.Text.RegularExpressions;
using System.Configuration;
using URLRewriter.Config;
using System.Data;
using System.Web;
using System.Web.UI;

namespace URLRewriter
{
/** <summary>
/// Provides a rewriting HttpModule.
/// </summary>
public class ModuleRewriter : BaseModuleRewriter
{
/** <summary>
/// This method is called during the module's BeginRequest event.
/// </summary>
/// <param name="requestedRawUrl">The RawUrl being requested (includes path and querystring).</param>
/// <param name="app">The HttpApplication instance.</param>
protected override void Rewrite(string requestedPath, System.Web.HttpApplication app)
{
//只对文件后缀为aspx页面有效
if (requestedPath.IndexOf(".aspx") != -1)
{
HttpContext httpContext = app.Context;

// 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(httpContext.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))
{
//aspx页面重定向到htm页面且http数据传输方式为“GET”
if (rules[i].Type == WebType.Static && app.Context.Request.RequestType == "GET")
{
//静态页面路径
string htmlWeb = RewriterUtils.ResolveUrl(httpContext.Request.ApplicationPath, rules[i].SendTo);
//静态页面物理路径
string htmPath = httpContext.Server.MapPath(htmlWeb);
//静态页面的最后修改时间
DateTime dt = System.IO.File.GetLastWriteTime(htmPath);
if (DateTime.Compare(DateTime.Now.AddHours(-1), dt) <= 0) //当前时间和静态页面生成时间对比,如果时间小于一个小时,重定向到静态页面
{
requestedPath = htmlWeb;
}
else //当前时间和静态页面生成时间对比,如果时间大于一个小时,设定Response筛选器,用于生成静态页面
{
httpContext.Response.Filter = new ResponseFilter(httpContext.Response.Filter, htmPath);
}
}
else
{
requestedPath = rules[i].SendTo;
}

// Rewrite the URL
RewriterUtils.RewriteUrl(httpContext, requestedPath);
break; // exit the for loop
}
}
}
}

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值