一个网站项目改版升级后用 httpModules 重写了URL,重写的名称是.htm 但部署时发现,网站上现有真实存在的.HTM文件确无法访问了。服务器环境是windows 2003 IIS6 。到GOOGLE上搜索一下找到如下解决办法。
在web.config文件中加入
1、在<compilation debug="true"> 节点加入
<buildProviders>
<add extension=".htm" type="System.Web.Compilation.PageBuildProvider"/>
</buildProviders>
2、在 <httpHandlers> 节点加入
<add verb="GET,POST" path="*.htm" type="System.Web.UI.PageHandlerFactory" validate="false"/>
这样修改后真实的.HTM页面是能显示了,但速度很慢,打开一个真实的.HTM页面都要3秒以上的时间。在到GOOGLE上搜了一圈,也没有找到答案。
本人就拿死马当活马医,自己继承了IHttpHandler 实现了一个HttpHandler处理,代码如下
代码
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Linq;
4
using
System.Text;
5
using
System.Web;
6
using
System.IO;
7
namespace
Aoecn.HttpHandler
8
{
9
///
<summary>
10
///
11
///
</summary>
12
public
class
HtmHandler: IHttpHandler
13
{
14
15
#region
IHttpHandler 成员
16
17
public
bool
IsReusable
18
{
19
get
{
return
true
; }
20
}
21
22
public
void
ProcessRequest(HttpContext context)
23
{
24
25
string
filepath
=
context.Request.Url.AbsolutePath;
26
filepath
=
filepath.Replace(
"
/
"
,
"
\\
"
);
27
filepath
=
string
.Concat(
@"
d:\web
"
,filepath);
28
if
(File.Exists(filepath)
==
false
)
29
{
30
filepath
=
@"d
:\web\404.html
"
;
31
}
32
string
tempstr;
33
using
(StreamReader sr
=
new
StreamReader(filepath, Encoding.UTF8))
34
{
35
tempstr
=
sr.ReadToEnd();
36
}
37
38
39
context.Response.Write(tempstr);
40
context.Response.End();
41
}
42
43
#endregion
44
}
45
}
46
然后修改web.config文件,把原来的 <add verb="GET,POST" path="*.htm" type="System.Web.UI.PageHandlerFactory" validate="false"/>
换成
<add verb="GET,POST" path="*.htm" type="Aoecn.HttpHandler.HtmHandler,Aoecn.HttpHandler" validate="false"/>
自己的HttpHandler就是把URL请求转换成服务器文件路径,然后在读取出来输出。没有想到的是速度相当的快。打开页面基本没有等待。而且重写部分的htm也真常显示。