而我们预期的结果应该如下图,实际只请求两次。

用301重定向可以解决该循环请求产生的问题。

OK, let’s begin.

本文的Demo和Source是基于上一篇的,如果下面的一些文件或文件夹没有提及创建的,表示已经在上一篇文章中创建过。

本文已经同步至我的个人博客站点:积累吧|ASP.NET 页面双向静态化

源代码下载:Routing-Static-Page-Demo-V2

Demo(点击这些链接会自动跳转到对应的.html页):

  • http://csdemo.jileiba.com
  • http://csdemo.jileiba.com/Default.aspx
  • http://csdemo.jileiba.com/Account/Login.aspx

1. 修改CustomRouteHandler类,添加RequestPath属性

using System.Web;using
System.Web.Compilation;using System.Web.Routing;using
System.Web.UI;namespace Routing_Static_Page_Demo.WebHandler{   
public class CustomRouteHandler
: IRouteHandler    {       
/// <summary>        /// 虚拟路径        /// </summary>       
public string VirtualPath {
get; private set; }       
/// <summary>        /// 请求路径        /// </summary>       
public string RequestPath        {           
get { return VirtualPath.Substring(1); }        }               
public CustomRouteHandler(string
virtualPath)        {            this.VirtualPath = virtualPath;        }       
/// <summary>        /// 返回实际请求页       
/// </summary>        public
IHttpHandler GetHttpHandler(RequestContext
requestContext)        {            foreach
(var urlParm in requestContext.RouteData.Values)            {                requestContext.HttpContext.Items[urlParm.Key] = urlParm.Value;            }           
var page = BuildManager.CreateInstanceFromVirtualPath(VirtualPath,
typeof(Page))
as IHttpHandler;           
return page;        }    }}

RequestPath属性是从VirtualPath过来的,如果VirtualPath为~/default.aspx,那么对应的RequestPath则是/default.aspx

2. 在WebModule下创建CustomHttpModule.cs类

using System;using
System.Globalization;using System.Web;using
System.Web.Routing;using Routing_Static_Page_Demo.WebHandler;namespace
Routing_Static_Page_Demo.WebModule{    public class
CustomHttpModule :
IHttpModule    {        private HttpApplication
app;        public void Init(HttpApplication
context)        {            app = context;            app.AuthorizeRequest += App_AuthorizeRequest;        }       
public void App_AuthorizeRequest(object
sender, EventArgs e)        {           
HttpRequest req = app.Request;           
string path = req.Path;            // 如果是.aspx页面           
if (path.EndsWith(".aspx",
true, CultureInfo.CurrentCulture))            {               
// routeUrl则用于存放对应的.html                string
routeUrl = string.Empty;               
// 遍历RouteTable,找到.aspx页面对应的.html                foreach
(Route route in
RouteTable.Routes)                {                   
// 获取CustomRouteHandler                    var
handler = (CustomRouteHandler) route.RouteHandler;                   
// 获取CustomRouteHandler的RequestPath                   
string requestPath = handler.RequestPath;                   
if (requestPath.ToLower() == path.ToLower())                    {                        routeUrl = route.Url;                       
break;                    }                }               
// 将.aspx页面永久重定向到对应的.html页面                app.Response.StatusCode = 301;                app.Response.AddHeader("Location",
"/" + routeUrl);                app.Response.End();            }        }       
public void Dispose()        {        }    }}

如果你不太熟悉HttpApplication的事件,可以参照:MSDN HttpApplication事件


如果你不太熟悉HttpApplication的用法,可以参照:MSDN HttpApplication类

3. 修改web.config文件,添加HttpModule配置

黄色标记的地方是添加的,其它配置不变。

<?xml version="1.0"
encoding="UTF-8"?><configuration>  <system.web>   
<compilation debug="true"
targetFramework="4.0"
/>    <httpModules>      <add
name="CustomHttpModule"
type="Routing_Static_Page_Demo.WebModule.CustomHttpModule, Routing_Static_Page_Demo"
/>    </httpModules>  </system.web>  <system.webServer>   
<modules runAllManagedModulesForAllRequests="true">      <remove
name="UrlRoutingModule"/>      <add
name="UrlRoutingModule"
type="System.Web.Routing.UrlRoutingModule,                                           System.Web,                                           Version=4.0.0.0,                                          
Culture=neutral,                                           PublicKeyToken=b03f5f7f11d50a3a"
/>      <add name="CustomHttpModule"
type="Routing_Static_Page_Demo.WebModule.CustomHttpModule"
/>    </modules>    <handlers>      <add
name="UrlRoutingHandler"                                  
preCondition="integratedMode"                                  
verb="*"
path="UrlRouting.axd"                                 
type="System.Web.HttpForbiddenHandler, System.Web,                                        Version=2.0.0.0, Culture=neutral,                                        PublicKeyToken=b03f5f7f11d50a3a"/>                     
</handlers>  </system.webServer></configuration>

在VS自带的WebDev服务器中运行这个项目:在浏览器栏输入http://localhost:xxxx/Default.aspx,会自动跳转到http://localhost:xxxx/Index.html,运行默认路径http://loclhost:xxxx/也会自动跳转到http://localhost:xxxx/Index.html。

4. 在IIS中运行项目

WebDev运行虽然通过了,IIS可不见得通过,毕竟WebDev的权限太高了。

果然,运行之后,出现下面的错误画面:

还是web.config的配置问题。在<webserver>节点下添加下面一行配置:

<validation validateIntegratedModeConfiguration="false"/>

这一行配置并不会真正影响web应用程序的安全性,它只是用于关闭有些配置将不会被使用的警告。

That’s end, have fun.nking, keep moving, even if the road obstacles , the one more important thing is that always be a pig for you, that's
keep fool.

ASP.NET 页面双向静态化的更多相关文章

  1. php 开启缓冲,页面纯静态化

    服务器默认不开启php缓冲区 两种方法开启 1.php.ini out_put_buffer = on 2.ob_start(); 页面纯静态化 file_put_contents()写文件 ob_s ...

  2. PHP 404页面/如何设置404页面/URL静态化/URL伪静态化

    php中如何设置404页面及其他错误页面 首先在项目根目录下新建文件,文件名为" .htaccess " 在该文件中写入一下配置项: ErrorDocument 404 /404. ...

  3. 使用Freemarker 实现JSP页面的静态化

    使用Freemarker 静态化网页 一.原理 Freemarker 生成静态页面,首先需要使用自己定义的模板页面,这个模板页面可以是最最普通的html,也可以是嵌套freemarker中的 取值表达 ...

  4. 一、springMVC、freemarker页面半自动静态化

    说明:刚刚接到公司的通知,实现(半自动化),即通过参数控制是否需要静态化页面(哪里我说错了,勿喷!谢谢) 1,请求.do的URL时直接生成对应的.htm文件,并将请求转发到该htm文件 2,自由控制某 ...

  5. asp.net mvc3的静态化实现

    静态化处理,可以大大提高客户的访问浏览速度,提高用户体验,同时也降低了服务器本身的压力.在asp.net mvc3中,可以相对容易地处理静态化问题,不用过多考虑静态网页的同步,生成等等问题.我提供这个 ...

  6. 如何做URL静态化 和页面的静态化

    为什么要进行URL静态化? 如果帮到了您,您可以小支持一下,谢谢您   1.更好的迎合搜索引擎工作原理的爬行抓取机制:2.把网站URL静态化更有助于网站获得好的排名:3.URL静态化有利于用户体验.不 ...

  7. 利用ResultFilter实现asp.net mvc 页面静态化

    为了提高网站性能.和网站的负载能力,页面静态化是一种有效的方式,这里对于asp.net mvc3 构架下的网站,提供一种个人认为比较好的静态话方式. 实现原理是通过mvc提供的过滤器扩展点实现页面内容 ...

  8. 利用ResultFilter实现asp.net mvc3 页面静态化

    为了提高网站性能.和网站的负载能力,页面静态化是一种有效的方式,这里对于asp.net mvc3 构架下的网站,提供一种个人认为比较好的静态话方式. 实现原理是通过mvc提供的过滤器扩展点实现页面内容 ...

  9. MVC页面静态化

    MVC 页面静态化   最近工作需要,实现页面静态化,以前在ASP时代,都是FSO自己手动生成的. 新时代,MVC了,当然也要新技术,网上一搜,找到一种解决方案,是基于MVC3的,实现原理是通过mvc ...

随机推荐

  1. FreeMarker template error: The following has evaluated to null or missing: ==> blogger.md [in template "admin/about.ftl" at line 44, column 84]

    FreeMarker template error:The following has evaluated to null or missing:==> blogger.md [in templ ...

  2. 洛谷 [P3384] 树链剖分 模版

    支持各种数据结构上树,注意取膜. #include <iostream> #include <cstring> #include <algorithm> #incl ...

  3. 如何使用JS实现banner图滚动

    通过JS实现banner图的滚动主要是定时器的应用 先新建好banner图的几张图片,最后一张与第一张用同一个,保证滚动的不间断 改好样式,需注意所有图片要在同行显示,否则不能向左滚动 声明一个函数, ...

  4. 安装RabbitMQ(二)

    RabbitMQ的简易安装 前一篇博文的RabbitMQ安装有点复杂,经过搜索发现简单的安装方式如下. 1.Erlang Yum Repos 基于 SSL 高版本包含插件 rpm -Uvh http: ...

  5. 关于目前自己iOS项目使用的第三方开源库

    1.AFNetworking 目前比较推荐的iOS网络请求组件,默认网络请求是异步,通过block回调的方式对返回数据进行处理. 2.FMDB 对sqlite数据库操作进行了封装,demo也比较简单. ...

  6. PHPUnit-附录 A. 断言 (assert)

    [http://www.phpunit.cn/manual/5.7/zh_cn/appendixes.assertions.html] 本附录列举可用的各种断言方法. assertArrayHasKe ...

  7. [翻译]编写高性能 .NET 代码 第一章:工具介绍 -- Performance Counters(性能计数器)

    <<返回目录 Performance Counters(性能计数器) 性能计数器是监视应用程序和系统性能的最简单的方法之一.它有几十个类别数百个计数器在,包括一些.net特有的计数器.要访 ...

  8. java-redis字符类数据操作示例(一)

    对于大部分程序猿来讲,学习新知识重在编码实践,于我也是这样.现在初识redis,一直看文章难免感觉是浮光掠影,印象不深.所以间隙中,将自己的测试代码整理成博客,旨在加深记忆并提醒自己对待编程要用心沉下 ...

  9. Java中Dom解析xml文档

    xml文档 <?xml version="1.0" encoding="UTF-8"?> <bookstore> <book id ...

  10. 第二十一章 Django的分页与cookie

    第二十一章 Django的分页与cookie 第一课 模板 1.模板的继承 在Template目录下新建模板master.html <!DOCTYPE html> <html lan ...