没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
原创|其它|编辑:郝浩|2009-04-24 09:25:57.000|阅读 998 次
概述:前段时间做项目,一直都在寻找一种动态生成htm页面的方法,要求配置简单,和项目无关。
功夫不负有心人,终于被我找到了,只需要在web.config中进行简单配置,就可以达到动态生成静态页面的效果,同时又不影响Url重定向。web.config中需要注意的配置节为
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
前段时间做项目,一直都在寻找一种动态生成htm页面的方法,要求配置简单,和项目无关。
功夫不负有心人,终于被我找到了,只需要 在web.config中进行简单配置,就可以达到动态生成静态页面的效果,同时又不影响Url重定向。web.config中需要注意的配置节 为<configuration>、<RewriteConfig>、<httpModules>、<httpHandlers>, 在这些配置节里边都有注释,容易看懂。
<?xml version="1.0" encoding="utf-8"?>
<!--
注意: 除了手动编辑此文件以外,您还可以使用
Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
“网站”->“Asp.Net配置”选项。
设置和注释的完整列表在
machine.config.comments 中,该文件通常位于
\Windows\Microsoft.Net\Framework\v2.x\Config 中
-->
<configuration>
<!-- RUL重写开始 -->
<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>
</configSections>
<RewriterConfig>
<Rules>
<!--地址重写规则-->
<!--首页,定位到静态页面-->
<RewriterRule>
<Type>Static</Type>
<LookFor>~/Default\.aspx</LookFor>
<SendTo>~/Default.htm</SendTo>
</RewriterRule>
<!--二级页面,定位到动态页面-->
<RewriterRule>
<Type>Dynamic</Type>
<LookFor>~/List\.aspx</LookFor>
<SendTo>~/Show.aspx</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
<!-- RUL重写结束 -->
<appSettings/>
<connectionStrings/>
<system.web>
<!--
设置 compilation debug="true" 将调试符号插入
已编译的页面中。但由于这会
影响性能,因此只在开发过程中将此值
设置为 true。
-->
<httpModules>
<!--URL重写-->
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
</httpModules>
<httpHandlers>
<!--生成静态页面-->
<add verb="*" path="*.aspx" validate="false" type="URLRewriter.RewriterFactoryHandler, URLRewriter"/>
</httpHandlers>
<compilation debug="false" />
<!--
通过 <authentication> 节可以配置 ASP.NET 使用的
安全身份验证模式,
以标识传入的用户。
-->
<authentication mode="Forms" />
<!--
如果在执行请求的过程中出现未处理的错误,
则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
开发人员通过该节可以配置
要显示的 html 错误页
以代替错误堆栈跟踪。
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<globalization requestEncoding="utf-8" responseEncoding="utf-8"/>
</system.web>
</configuration>
两个关键的类是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
{
/**//// <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);
//静态页面的最后修改时间
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
{
/**//// <summary>
/// 依据web.config配置信息,生成静态页面
/// </summary>
public class CreateHtmFactoryHandler : IHttpHandlerFactory
{
/**//// <summary>
///
/// </summary>
/// <param name="context">HttpContext 类的实例,它提供对用于为 HTTP 请求提供服务的内部服务器对象(如 Request、Response、Session 和 Server)的引用</param>
/// <param name="requestType">客户端使用的 HTTP 数据传输方法(GET 或 POST)</param>
/// <param name="url">所请求资源的 RawUrl。</param>
/// <param name="pathTranslated">所请求资源的 PhysicalApplicationPath</param>
/// <returns>返回实现 IHttpHandler 接口的类的实例</returns>
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)
{
}
}
}
以上是两个主要的类,还有一些辅助的类,我把测试项目附上,里边有源代码和示例。
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com
文章转载自:自互联网面对“数字中国”建设和中国制造2025战略实施的机遇期,中车信息公司紧跟时代的步伐,以“集约化、专业化、标准化、精益化、一体化、平台化”为工作目标,大力推进信息服务、工业软件等核心产品及业务的发展。在慧都3D解决方案的实施下,清软英泰建成了多模型来源的综合轻量化显示平台、实现文件不失真的百倍压缩比、针对模型中的大模型文件,在展示平台上进行流畅展示,提升工作效率,优化了使用体验。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@evget.com
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
慧都科技 版权所有 Copyright 2003-
2025 渝ICP备12000582号-13 渝公网安备
50010702500608号