All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
SQL注入天书之ASP注入漏洞全接触_[SQL,Server教程]
ASP.NET技巧:access下的分页方案_[Asp.Net教程]
用Php模拟asp.net的页面模型_php资料_编程技术
十九 控制文本 Mastering Text_[Html教程]
IsPostBack深入探讨_[Asp.Net教程]
Asp.Net,动态生成验证码_[Asp.Net教程]
使用javascript创建快捷方式_JavaScript技术_编程技术
菜鸟的最爱,.NET经典代码汇总(五)_.net资料_编程技术
使用PHP通过SMTP发送邮件新手指南_php资料_编程技术
动态网页常用技术:用PHP追随变量(cookie和session)_php资料_编程技术
Reading number is top 10 pictures
新版武松打虎
30 beautiful school beauty4
The money of more than 100 countries and regions9
BingBingFan apple dew point photo gallery4
中国文革时期的色情图片1
Forced sex girl living abroad1
狗狗与主人神同步1
浴室里的美女
Chinese paper-cut grilles art appreciation2
俄罗斯台球天后惊艳魅惑1
Download software ranking
WebService在.NET中的实战应用教学视频 → 第3集
Call Of Duty2
Unix video tutorial20
超级战舰
Unix video tutorial4
天龙八部十二宫服务端
Unix video tutorial12
电车之狼R
Desire a peach blossom
WebService在.NET中的实战应用教学视频 → 第2集
归海一刀 published in(发表于) 2014/1/30 1:09:21 Edit(编辑)
利用UrlRewrite,asp.net动态生成htm页面(补充说明2)_[Asp.Net教程]

利用UrlRewrite,asp.net动态生成htm页面(补充说明2)_[Asp.Net教程]

利用UrlRewrite,asp.net动态生成htm页面(补充说明2)_[Asp.Net教程]

前几天写了两篇关于URL重写时,生成静态页面的随笔。


利用UrlRewrite,asp.net动态生成htm页面
利用UrlRewrite,asp.net动态生成htm页面(补充说明)
今天把原来的思路给整理了一下,原来需要两个类(ModuleRewriter和CreateHtmFactoryHandler)才能完成整个过程,现在只用ModuleRewriter就可以了,我画了个流程图


关键类ModuleRewriter代码


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


namespace URLRewriter
{
/**////


/// Provides a rewriting HttpModule.
///

public class ModuleRewriter : BaseModuleRewriter
{
/**////
/// This method is called during the module's BeginRequest event.
///

/// The RawUrl being requested (includes path and querystring).
/// The HttpApplication instance.
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
}
}
}
}


}
}

原代码和演示示例 下载


来源:http://www.cnblogs.com/fengfeng







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.