ashx文件及其作用
微软文档
------------------------------
在 ASP.NET 中,主要使用两种方法来创建 HTTP 处理程序。第一种是通过创建带有 ASHX 扩展名的文件,另一种是创建实现 System.Web.IHttpHandler 的类,请参阅 IHttpHandler Interface。本文将着重介绍第二种形式。要创建 HTTP 处理程序,需要创建一个程序集(通常是一个代码库项目)和一个实现 System.Web.IHttpHandler 的类。然后将该类注册到 Web.Config(或 machine.config)文件中,然后它就可以接收请求了。如果查看 machine.config 文件(在相应命名的 httpHandlers 节中),将看到许多当前已注册的 HTTP 处理程序,包括 System.Web.UI.PageHandlerFactory(ASP.NET 页面的主处理程序)。在编写 HTTP 处理程序时,其实就是在定义处理请求的新方法。
-------------------------------------
aspx也可以实现相同的效果,但稍微麻烦一点,需要更改web.config文件.
using System.Web
public sealed class TextBuilder : IHttpHandler

{
// Override the ProcessRequest method.
public void ProcessRequest(HttpContext context)
{
context.Response.ClearContent();
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
context.Response.End();
}
// Override the IsReusable property.
public bool IsReusable
{
get
{ return true; }
}
}
------------------------------
在 ASP.NET 中,主要使用两种方法来创建 HTTP 处理程序。第一种是通过创建带有 ASHX 扩展名的文件,另一种是创建实现 System.Web.IHttpHandler 的类,请参阅 IHttpHandler Interface。本文将着重介绍第二种形式。要创建 HTTP 处理程序,需要创建一个程序集(通常是一个代码库项目)和一个实现 System.Web.IHttpHandler 的类。然后将该类注册到 Web.Config(或 machine.config)文件中,然后它就可以接收请求了。如果查看 machine.config 文件(在相应命名的 httpHandlers 节中),将看到许多当前已注册的 HTTP 处理程序,包括 System.Web.UI.PageHandlerFactory(ASP.NET 页面的主处理程序)。在编写 HTTP 处理程序时,其实就是在定义处理请求的新方法。
-------------------------------------
aspx也可以实现相同的效果,但稍微麻烦一点,需要更改web.config文件.
打开一个Web项目,然后在任意目录下使用VS2005解决方案资源管理器的“添加”-->“添加新项”,在对话框中选择“文本文件”,然后在文件名处输入“TextBuilder.ashx”
1
<%@ WebHandler language="C#" Class="MyNamespace.TextBuilder" codebehind="TextBuilder.ashx.cs" %>
<%@ WebHandler language="C#" Class="MyNamespace.TextBuilder" codebehind="TextBuilder.ashx.cs" %> 新建类TextBuilder.ashx.cs
using System.Web
public sealed class TextBuilder : IHttpHandler 
{
// Override the ProcessRequest method.
public void ProcessRequest(HttpContext context)
{
context.Response.ClearContent();
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
context.Response.End();
}
// Override the IsReusable property.
public bool IsReusable
{
get
{ return true; }
}
} 或者你可以查看浏览器信息
1
// Name this C# file HandlerTest.cs and compile it with the
2
// command line: csc /t:library /r:System.Web.dll HandlerTest.cs.
3
// Copy HandlerTest.dll to your \bin directory.
4
5
using System.Web;
6
7
namespace HandlerExample
8
{
9
public class MyHttpHandler : IHttpHandler
10
{
11
// Override the ProcessRequest method.
12
public void ProcessRequest(HttpContext context)
13
{
14
context.Response.Write("<H1>This is an HttpHandler Test.</H1>");
15
context.Response.Write("<p>Your Browser:</p>");
16
context.Response.Write("Type: " + context.Request.Browser.Type + "<br>");
17
context.Response.Write("Version: " + context.Request.Browser.Version);
18
}
19
20
// Override the IsReusable property.
21
public bool IsReusable
22
{
23
get { return true; }
24
}
25
}
26
}
27
// Name this C# file HandlerTest.cs and compile it with the2
// command line: csc /t:library /r:System.Web.dll HandlerTest.cs.3
// Copy HandlerTest.dll to your \bin directory.4

5
using System.Web;6

7
namespace HandlerExample8
{9
public class MyHttpHandler : IHttpHandler10
{11
// Override the ProcessRequest method.12
public void ProcessRequest(HttpContext context)13
{14
context.Response.Write("<H1>This is an HttpHandler Test.</H1>"); 15
context.Response.Write("<p>Your Browser:</p>");16
context.Response.Write("Type: " + context.Request.Browser.Type + "<br>");17
context.Response.Write("Version: " + context.Request.Browser.Version);18
}19

20
// Override the IsReusable property.21
public bool IsReusable22
{23
get { return true; }24
}25
}26
}27


浙公网安备 33010602011771号