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

Reading number is top 10 articles
ASP.NET2.0新特性概述_[Asp.Net教程]
学习PHP:详解mysql4.0以后的编码配置_php资料_编程技术
利用同义词简化SQL Server 2005开发_[SQL Server教程]
PHP开发中接收复选框信息的方法_[PHP教程]
asp.net2.0母版页的概述
编写数据库操作类,使ASP.NET中的数据库操作变得简单_[Asp.Net教程]
ASP.NET获取电脑名、IP及用户名_[Asp.Net教程]
使用C#来开发.NET,CF,蓝牙通信模块_.net资料_编程技术
ajax+asp.net2.0,sql2005三级联下拉框_[Asp.Net教程]
C#中水晶报表简介
Reading number is top 10 pictures
人美胸美腿更美2
Female model behind the bitterness, often being overcharged4
So beauty, will let you spray blood3
2012 national geographic daily picture3
YangYuYing and ZhengShaoQiu dance on the generous come interest dye-in-the-wood
Angie Chiu vijara myth1
Seductive beauty of crime1
中国女孩大胆自拍,显露完美身材4
可爱的小动物
Fat bird to fly
Download software ranking
虚拟机汉化软件
VC++6.0简体中文版
Unix video tutorial10
Tram sex maniac 2 (H) rar bag13
C#COM编程指南
Eclipse-CALMSANNY (second edition)
Such love down(擒爱记)
c#程序设计案例教程
Sora aoi, the maid, students' uniforms
VC++6.0培训教程
归海一刀 published in(发表于) 2014/1/30 1:09:18 Edit(编辑)
利用UrlRewrite,asp.net动态生成htm页面_[Asp.Net教程]

利用UrlRewrite,asp.net动态生成htm页面_[Asp.Net教程]

利用UrlRewrite,asp.net动态生成htm页面_[Asp.Net教程]

前段时间做项目,一直都在寻找一种动态生成htm页面的方法,要求配置简单,和项目无关。
功夫不负有心人,终于被我找到了,只需要在web.config中进行简单配置,就可以达到动态生成静态页面的效果,同时又不影响Url重定向。web.config中需要注意的配置节为,在这些配置节里边都有注释,容易看懂。
















Static
~/Default\.aspx
~/Default.htm




Dynamic
~/List\.aspx
~/Show.aspx































两个关键的类是ModuleRewriter和RewriterFactoryHandler
ModuleRewriter类用于Url重定向,代码如下:
ModuleRewriter
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);
//静态页面的最后修改时间
DateTime dt = System.IO.File.GetLastWriteTime(httpContext.Server.MapPath(htmlWeb));
//当前时间和静态页面生成时间对比,如果时间小于一个小时,重定向到静态页面
if (DateTime.Compare(DateTime.Now.AddHours(-1), dt) <= 0)
{
requestedPath = htmlWeb;
}
}
else
{
requestedPath = rules[i].SendTo;
}


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


}
}
CreateHtmFactoryHandler类用于生成静态页面,代码如下:
CreateHtmFactoryHandler
using System;
using System.IO;
using System.Web.UI;
using System.Web;
using URLRewriter.Config;
using System.Configuration;
using System.Text.RegularExpressions;


namespace URLRewriter
{
/**////


/// 依据web.config配置信息,生成静态页面
///

public class CreateHtmFactoryHandler : IHttpHandlerFactory
{
/**////
///
///

/// HttpContext 类的实例,它提供对用于为 HTTP 请求提供服务的内部服务器对象(如 Request、Response、Session 和 Server)的引用
/// 客户端使用的 HTTP 数据传输方法(GET 或 POST)
/// 所请求资源的 RawUrl。
/// 所请求资源的 PhysicalApplicationPath
/// 返回实现 IHttpHandler 接口的类的实例
public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
{
if (requestType == "GET")
{
// 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(context.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(url) && rules[i].Type == WebType.Static)
{
//得到要生成的静态页面的物理路径
string physicsWeb = context.Server.MapPath(RewriterUtils.ResolveUrl(context.Request.ApplicationPath, rules[i].SendTo));
//设定Response筛选器
context.Response.Filter = new ResponseFilter(context.Response.Filter, physicsWeb);
break;
}
}
}
//得到编译实例
return PageParser.GetCompiledPageInstance(url, pathTranslated, context);
}


public virtual void ReleaseHandler(IHttpHandler handler)
{
}
}
}


以上是两个主要的类,还有一些辅助的类,我把测试项目附上,里边有源代码和示例。
点击这里下载:源代码和示例
声明一下,我是在修改一个开源的项目(UrlRewrite)来实现上述功能。


来源: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.