Generic Handler is straight forward in executing functionality which can also be achieved using server page (aspx). The advantage with generic handler, is that it does with out executing the page life cycle events. The response from generic handler is same as like a server page which means it can be rendered in a browser
Below example is to render an image using generic handler.
Generic Handler:
<%@ WebHandler Language="C#" Class="MyHandler" %>
using System;
using System.Web;
public class MyHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/png";
context.Response.WriteFile(context.Server.MapPath("Images/Google.png"));
}
public bool IsReusable
{
get
{
return false;
}
}
}
ASPX Page :
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<img src="MyHandler.ashx" />
</asp:Content>
The generic handler page can also be requested directly from the browser beside from a server page.
No comments:
Post a Comment