1 using System; 2 using System.Web; 3 4 public class helloZZ : IHttpHandler { 5 6 public void PRocessRequest (HttpContext context) { 7 context.Response.ContentType = "text/plain"; 8 context.Response.Write("你請求的是hello.zz文件"); 9 }10 11 public bool IsReusable {12 get {13 return false;14 }15 }16 17 }
再在a目錄下建立handler.ashx,代碼如下:
<%@ WebHandler Language="C#" Class="MyHandler" %>using System;using System.Web;public class MyHandler : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.Write("Hello World"); } public bool IsReusable { get { return false; } }}
再建立如下的web.config
<?xml version="1.0"?><configuration> <system.web> <compilation debug="false" targetFramework="4.0" /> <httpHandlers> <add path="hello.zz" verb="*" type="helloZZ"/> </httpHandlers> </system.web></configuration>
特殊說明:
請直接用vs2012打開handler.ashx,右鍵用瀏覽器打開,這樣做的只是為了構建一個web環境。再請求hello.zz就可以了
新聞熱點
疑難解答