ashx 文件怎么用

ashx 文件怎么用本文导读 ashx 是什么文件 如何创建 ashx 文件用于写 webhandler 的 其实就是带 HTML 和 C 的混合文件 ashx 文件类似于 aspx 文件 可以通过它来调用 HttpHandler 类 从而免去了普通 aspx 页面的控件解析以及页面处理的过程 一 ashx 文件的添加打开你的 ASP NETwebsite 右击项目选择 AddNewItem 将显示

本文导读:ashx是什么文件,如何创建 .ashx 文件用于写web handler的。其实就是带HTML和C#的混合文件。.ashx文件类似于.aspx文件,可以通过它来调用HttpHandler类,从而免去了普通.aspx页面的控件解析以及页面处理的过程。

一、ashx文件的添加

打开你的ASP.NET web site;右击项目选择 “Add New Item…”;将显示一个“Add New Item”的对话框,选择“Generic Handler”。此时,你就会得到一个新的ashx文件。

 

二、ashx文件自动生成的代码

 

它定义了IHttpHandler接口的两部分。非常重要的一部分是ProcessRequest(),它将决定这个ashx文件是被请求还是被显示。你不能修改这个继承的接口或删除它的方法。

 
C# 代码   
复制
 ashx 文件怎么用<%@ WebHandler Language="C#" Class="Handler" %> ashx 文件怎么用 ashx 文件怎么用using System; ashx 文件怎么用using System.Web; ashx 文件怎么用 ashx 文件怎么用public class Handler : IHttpHandler ashx 文件怎么用{ ashx 文件怎么用 public void ProcessRequest (HttpContext context) ashx 文件怎么用 { ashx 文件怎么用 context.Response.ContentType = "text/plain"; ashx 文件怎么用 context.Response.Write("Hello World"); ashx 文件怎么用 } ashx 文件怎么用 ashx 文件怎么用 public bool IsReusable ashx 文件怎么用 { ashx 文件怎么用 get { return false; } ashx 文件怎么用 } ashx 文件怎么用}

 

从以上代码我们可以发现,一般处理程序是一个实现了IHttpHandler接口的类,可以在服务器端执行,必然也可以从浏览器获得数据,也可以发给浏览器数据,那么上面的代码各自都代表什么呢?

 

ProcessRequest (HttpContext context):方法在程序被访问时调用,参数是请求上下文的对象,通过对象可以处理信息

context.Response.Write(“Hello World”):是向浏览器输出方法,把数据从服务器发送到浏览器。

 

三、ashx文件相关知识说明

HttpHandler是一个HTTP请求的真正处理中心,也正是在这个HttpHandler容器中,ASP.NET Framework才真正地对客户端请求的服务器页面做出编译和执行,并将处理过后的信息附加在HTTP请求信息流中再次返回到HttpModule中。

 

2、IHttpHandler是什么

IHttpHandler定义了如果要实现一个HTTP请求的处理所必需实现的一些系统约定。HttpHandler与HttpModule不同,一旦定义了自己的HttpHandler类,那么它对系统的HttpHandler的关系将是“覆盖”关系。

 

3、IHttpHandler如何处理HTTP请求

当一个HTTP请求经同HttpModule容器传递到HttpHandler容器中时,ASP.NET Framework会调用HttpHandler的ProcessRequest成员方法来对这个HTTP请求进行真正的处理。以一个ASPX页面为例,正是在这里一个ASPX页面才被系统处理解析,并将处理完成的结果继续经由HttpModule传递下去,直至到达客户端。

对于ASPX页面,ASP.NET Framework在默认情况下是交给System.Web.UI.PageHandlerFactory这个HttpHandlerFactory来处理的。所谓一个HttpHandlerFactory,所谓一个HttpHandlerFactory,是指当一个HTTP请求到达这个HttpHandler Factory时,HttpHandlerFactory会提供出一个HttpHandler容器,交由这个HttpHandler容器来处理这个HTTP请求。

一个HTTP请求都是最终交给一个HttpHandler容器中的ProcessRequest方法来处理的。

 

四、ashx文件的实例

 

前端页面

 
HTML 代码   
复制
 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ajax。aspx。cs" Inherits="About_ajax" %> ashx 文件怎么用 ashx 文件怎么用 
       DOCTYPE> ashx 文件怎么用 ashx 文件怎么用<html> ashx 文件怎么用<head runat="server"> ashx 文件怎么用 <title>Jquery Ajax实例 
        title> ashx 文件怎么用 <script type="text/javascript"> ashx 文件怎么用 $(document).ready(function() { ashx 文件怎么用 $("#dbtn").click(function() { ashx 文件怎么用 $.ajax({ ashx 文件怎么用 type: "POST", ashx 文件怎么用 //dataType:"Text",  ashx 文件怎么用 url: "AjaxHandler。ashx", ashx 文件怎么用 data: { name: "admin", pass: "admin" }, ashx 文件怎么用 beforeSend: function() { $("#ds").html("loading"); }, ashx 文件怎么用 success: function(msg) { $("#ds").html("

" + msg + ""); }
ashx 文件怎么用 }
); ashx 文件怎么用 }
); ashx 文件怎么用 // ashx 文件怎么用 $("#btn_cbfbh").click(function() { ashx 文件怎么用 //var zbm = '111'; ashx 文件怎么用 //alert(zbm); ashx 文件怎么用 $.ajax({ ashx 文件怎么用 type: "POST", ashx 文件怎么用 //dataType:"Text", ashx 文件怎么用 url: "S_CBFBM。ashx", ashx 文件怎么用 data: { ZBM: "200" }, ashx 文件怎么用 beforeSend: function() { ashx 文件怎么用 //$("#div_load").visible = true; ashx 文件怎么用 }, ashx 文件怎么用 success: function(msg) { ashx 文件怎么用 //$("#div_load").visible = false; ashx 文件怎么用 $("#ds").html("

" + msg + ""); ashx 文件怎么用 $("#CBFBM").val(msg); ashx 文件怎么用 }
ashx 文件怎么用 }
); ashx 文件怎么用 }
); ashx 文件怎么用 // ashx 文件怎么用 }
); ashx 文件怎么用 function js_function_get_cbfbm(p_zdm) { ashx 文件怎么用 $.ajax({ ashx 文件怎么用 type: "POST", ashx 文件怎么用 url: "S_CBFBM。ashx", ashx 文件怎么用 data: { ZBM: p_zdm }, ashx 文件怎么用 beforeSend: function() { ashx 文件怎么用 //$("#div_load").visible = "true; ashx 文件怎么用 }, ashx 文件怎么用 success: function(msg) { ashx 文件怎么用 //$("#div_load").visible = false; ashx 文件怎么用 $("#ds").html("

" + msg + ""); ashx 文件怎么用 $("#CBFBM").val(msg); ashx 文件怎么用 }
ashx 文件怎么用 }
); ashx 文件怎么用 }
; ashx 文件怎么用
script> ashx 文件怎么用 head> ashx 文件怎么用<body onload="javascript:{js_function_get_cbfbm('sfsfds');}"> ashx 文件怎么用 <form id="form1" runat="server"> ashx 文件怎么用 <div> ashx 文件怎么用 <div id="ds"><p>我是AJAX原来的文字! p> div> ashx 文件怎么用 <input type="button" value="提交AJAX测试" id="dbtn" name="dbtn" /> ashx 文件怎么用 <br /> ashx 文件怎么用 <input type="text" id="CBFBM" name="CBFBM"/> ashx 文件怎么用 <input type="button" value="获取成包方编号" id="btn_cbfbh" name="btn_cbfbh" /> ashx 文件怎么用 <br /> ashx 文件怎么用 <div id="div_load" ><p> p> div> ashx 文件怎么用 div> ashx 文件怎么用 <script type="text/javascript"> ashx 文件怎么用 var zbm=''; ashx 文件怎么用 if(zbm=='') zbm='900'; ashx 文件怎么用 //js_function_get_cbfbm(zbm); ashx 文件怎么用 script> ashx 文件怎么用 form> ashx 文件怎么用 body> ashx 文件怎么用 html> ashx 文件怎么用

 

后端类代码

 
C# 代码   
复制
 ashx 文件怎么用<%@ WebHandler Language="C#" Class="AjaxHandler" %> ashx 文件怎么用 ashx 文件怎么用using System; ashx 文件怎么用using System.Web; ashx 文件怎么用 ashx 文件怎么用public class AjaxHandler : IHttpHandler { ashx 文件怎么用 ashx 文件怎么用 public void ProcessRequest (HttpContext context) { ashx 文件怎么用 //context.Response.ContentType = "text/plain"; ashx 文件怎么用 //context.Response.Write("Hello World"); ashx 文件怎么用 context.Response.ContentType = "text/plain"; ashx 文件怎么用 ashx 文件怎么用 //context.Response.Write("Hello World");  ashx 文件怎么用 if (context.Request["name"].ToString() == "admin" && ashx 文件怎么用 context.Request["pass"].ToString() == "admin") ashx 文件怎么用 { ashx 文件怎么用 context.Response.Write("Y"); ashx 文件怎么用 } ashx 文件怎么用 else ashx 文件怎么用 { ashx 文件怎么用 context.Response.Write("N"); ashx 文件怎么用 } ashx 文件怎么用 } ashx 文件怎么用 ashx 文件怎么用 public bool IsReusable { ashx 文件怎么用 get { ashx 文件怎么用 return false; ashx 文件怎么用 } ashx 文件怎么用 } ashx 文件怎么用 ashx 文件怎么用 [System.Web.Services.WebMethod] ashx 文件怎么用 public static string SayHello() ashx 文件怎么用 { ashx 文件怎么用 return "Hello Ajax! AjaxHandler。ashx"; ashx 文件怎么用 } ashx 文件怎么用 ashx 文件怎么用}

 

 
C# 代码   
复制
ashx 文件怎么用<%@ WebHandler Language="C#" Class="S_CBFBM" %> ashx 文件怎么用 ashx 文件怎么用using System; ashx 文件怎么用using System.Web; ashx 文件怎么用 ashx 文件怎么用///  ashx 文件怎么用/// 功能:获取 编号 ashx 文件怎么用///  ashx 文件怎么用public class S_CBFBM : IHttpHandler { ashx 文件怎么用 ashx 文件怎么用 public void ProcessRequest (HttpContext context) ashx 文件怎么用 { ashx 文件怎么用 context.Response.ContentType = "text/plain"; ashx 文件怎么用 ashx 文件怎么用 string zbm=context.Request["ZBM"].ToString(); ashx 文件怎么用 zbm=zbm.Trim(); ashx 文件怎么用 if(zbm!="") ashx 文件怎么用 { ashx 文件怎么用 string cbfbm = zbm + "001"; ashx 文件怎么用 context.Response.Write(cbfbm); ashx 文件怎么用 } ashx 文件怎么用 else ashx 文件怎么用 { ashx 文件怎么用 context.Response.Write(""); ashx 文件怎么用 } ashx 文件怎么用 } ashx 文件怎么用 ashx 文件怎么用 public bool IsReusable ashx 文件怎么用 { ashx 文件怎么用 get ashx 文件怎么用 { ashx 文件怎么用 return false; ashx 文件怎么用 } ashx 文件怎么用 } ashx 文件怎么用 ashx 文件怎么用 [System.Web.Services.WebMethod] ashx 文件怎么用 public static string SayHello() ashx 文件怎么用 { ashx 文件怎么用 return "Hello Ajax! S_CBFBM。ashx"; ashx 文件怎么用 } ashx 文件怎么用 ashx 文件怎么用}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/201323.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月20日 上午9:35
下一篇 2026年3月20日 上午9:36


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号