HTTP Handler применяется в тех случаях, когда на стороне сервера должны быть выполнены какие либо действия, без возвращения разметки страницы.
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta charset="utf-8" /> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Text" OnClick="Button1_Click" /> <asp:Button ID="Button2" runat="server" Text="Image" OnClick="Button2_Click" /> </div> </form> </body> </html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta charset="utf-8" /> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Text" OnClick="Button1_Click" /> <asp:Button ID="Button2" runat="server" Text="Image" OnClick="Button2_Click" /> </div> </form> </body> </html>
Default.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { Response.Redirect("~/HandlerText.ashx"); } protected void Button2_Click(object sender, EventArgs e) { Response.Redirect("~/HandlerImage.ashx"); } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { Response.Redirect("~/HandlerText.ashx"); } protected void Button2_Click(object sender, EventArgs e) { Response.Redirect("~/HandlerImage.ashx"); } }
HandlerText.ashx
<%@ WebHandler Language="C#" Class="HandlerText" %> using System; using System.Web; public class HandlerText : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.Write("Hello World"); } public bool IsReusable { get { return false; } } }
<%@ WebHandler Language="C#" Class="HandlerText" %> using System; using System.Web; public class HandlerText : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.Write("Hello World"); } public bool IsReusable { get { return false; } } }
HandlerImage.ashx
<%@ WebHandler Language="C#" Class="HandlerImage" %> using System; using System.Web; public class HandlerImage : IHttpHandler { public void ProcessRequest(HttpContext context) { string path = context.Server.MapPath("~/img/home.jpg"); context.Response.ContentType = "image/jpg"; context.Response.WriteFile(path); } public bool IsReusable { get { return false; } } }
<%@ WebHandler Language="C#" Class="HandlerImage" %> using System; using System.Web; public class HandlerImage : IHttpHandler { public void ProcessRequest(HttpContext context) { string path = context.Server.MapPath("~/img/home.jpg"); context.Response.ContentType = "image/jpg"; context.Response.WriteFile(path); } public bool IsReusable { get { return false; } } }