我们这里介绍一下在IIS6下开发的文件到IIS7上以后出现的问题我们该如何处理,我们将一步一步分析并解决问题。下面是一个监控服务器状态的页面,内容很简单,就是显示一下服务器当前CPU和内存的使用情况。因为想让它每3秒刷新一次,所以就把功能放在了ashx页里面,页面每3秒调用一次。
$(document).ready(function() { loaddata(); setInterval(loaddata, 3000); }); function loaddata() { $.getJSON("Common/ServerInfoHandler.ashx?format=json&jsoncallback=?", {}, function(json) { $("#labServerName").text("服务器名称:" + json.ServerName); $("#labServerIP").text("服务器地址:" + json.ServerIP); $("#NumberOfProcessors").text("CPU数量:" + json.CPUNum); $("#ProcessorType").text("CPU类型:" + json.CPUType); $("#ProcessorLevel").text("CPU级别:" + json.CPULevel); $("#MemoryLoad").text("内存使用率:" + json.MemoryLoad); $("#TotalPhys").text("物理内存共有:" + json.MemoryTotalPhys); $("#AvailPhys").text("可使用的物理内存有:" + $("#Date").text("服务器时间:" + json.DateTime); $("#cpu").text("CPU平均使用率:" + json.CPULoad); }); }
东西很简单,写完以后用VisualStudio直接运行,发现取到并显示出来的服务器名和地址都是127.0.0.1,所以就想在IIS上部署一下看看。开发的机器是Win7,自带的是IIS7,跟IIS6差别太大了。好不容易部署好了以后,浏览页面发现没有任何信息显示出来,在Visual Studio开调试,发现ashx页根本没有被调用。
下面我们说说解决办法:
就是在IIS7中你的网站的

里面右上角

“添加托管处理程序”中加上你的ashx。

这里直接在IIS里面加了,发现还是不好使。下面说说配置:
代码
<system.webServer> <handlers> <addname="Custom Handler" path="*.sample"verb="*" type="HelloWorldHandler" resourceType="Unspecified" /> </handlers> </system.webServer>
拷过来再说,扔进我的web.config改吧改吧,你要用Synchronous HTTPHandlers 不是建ashx了,而是在App_Code里建一个类,然后继承、实现IHttpHandler
代码
usingSystem.Web;
public class HelloWorldHandler : IHttpHandler { public HelloWorldHandler() { } public voidProcessRequest(HttpContext context) { HttpRequestRequest = context.Request; HttpResponse Response = context.Response; // This handler is called whenever a file ending // in .sample is requested. A file with that extension // does not need to exist. Response.Write("Hello Handler!"); } public boolIsReusable { // To enable pooling, return true here. // This keeps the handler in memory. get { return false; } } }
然后呢,配置web.config。这里比较麻烦,有三种配置方式。
IIS6:
<configuration> <system.web> <httpHandlers> <add verb="*" path="*.sample" type="HelloWorldHandler"/> </httpHandlers> </system.web> </configuration>
IIS7是分两种工作模式经典模式和集成模式,所以配置也是分两种的。
经典模式是为了与之前的版本兼容,使用ISAPI扩展来调用ASP.NET运行库,原先运行于IIS6.0下的Web应用程序迁移到IIS7.0中只要将应用程序配置成经典模式,代码基本不用修改就可以正常运行。
配置如下
代码
<configuration> <system.web> <httpHandlers> <add verb="*" path="*.sample" type="HelloWorldHandler"/> </httpHandlers> </system.web> <system.webServer> <handlers> <add verb="*" path="*.sample" name="HelloWorldHandler" type="HelloWorldHandler" modules="IsapiModule"/> </handlers> </system.webServer> </configuration>
集成模式是一种统一的哀求处理管道,它将ASP.NET请求管道与IIS核心管道组合在一起,这种模式能够提供更好的性能,能够实现配置和治理的模块化,而且增加了使用托管代码模块扩展IIS时的灵活性。
配置
代码
<configuration> <system.webServer> <handlers> <add verb="*" path="*.sample" name="HelloWorldHandler" type="HelloWorldHandler"/> </handlers> </system.webServer> </configuration>
关于配置我要解释几句,name和type就不用说,verb不知道啥意思,照写就行;path这个节里面的值是可以指定的,现在这样*.sample在测试的时候,你就要指向test.Sample。如果你有多个Handler的话就不能这么指定了,最好是指定一个名字比如:
<add verb="*" path="HelloWorld.sample" name="HelloWorldHandler" type="HelloWorldHandler"/> <add verb="*" path="HelloHeaven.sample" name="HelloHeavenHandler" type="HelloHeavenHandler"/>
写完配置以后就可以测试一下自己的Handler了,
测试地址如下
http://localhost/HttpHandler/test.sample
当然你要是设置了名字就可以用
http://localhost/HttpHandler/ HelloWorld.sample http://localhost/HttpHandler/ HelloHeaven.sample
用的时候也就变成了
$.getJSON("HandlerHelloWorld.sample?format=json&jsoncallback=?",……
下面是本文的疑问?用类写Handler的话那ashx呢?怎么用啊?
添加新文件的时候一般处理程序(ashx)还是可以选的。
还有就是iis6和iis7要写不同的三种配置,有没有通用的写法?不用具体非的指定某种IIS。
(责任编辑:admin) |