Please navigate to the bottom of the page for Table of Contents

Thursday, June 2, 2011

ASP.NET HttpHandlers

An ASP.NET HTTP handler is a process (frequently referred to as the "endpoint") that runs in response to a request made to an ASP.NET Web application. The most common handler is an ASP.NET page handler that processes .aspx files. When users request an .aspx file, the request is processed by the page through the page handler. You can create your own HTTP handlers that render custom output to the browser. Using HttpHandlers to serve up content you might normally serve using a standard ASP.NET page (such
as a dynamic file download request) can be a good idea in your application because it allows you to write a
specialized handler that eliminates some of the overhead of a standard ASP.NET handler. HttpHandlers are best suited for generating content such as RSS feeds and serving images.

Let’s write a simple HttpHandler that will return a requested image to the user. To add a handler to your web project, right click on your web project in solution explorer, select Add, New Item… and then select a Generic Handler. Visual Studio generates all the required plumbing for you as shown below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication2
{
/// <summary>
/// Summary description for Handler1
/// </summary>
public class Handler1 : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
}

public bool IsReusable
{
get
{
return false;
}
}
}
}


As it is apparent, the ProcessRequest method is the method you use to actually process the incoming HTTP request. Now let’s modify this code to serve up images requested by the user as a query string parameter. This would require us to parse the query string, change the content type and stream out the requested file as the response. Note that this example does not do error handling or content caching.


using System.Web;

namespace WebApplication2
{
/// <summary>
/// Summary description for Handler1
/// </summary>
public class Handler1 : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
// default to a generic image
string imageName = "GenericImage.jpg";

// parse user request
if (!string.IsNullOrEmpty(context.Request.QueryString["img"]))
imageName = context.Request.QueryString["img"];

// send the image file
context.Response.ContentType = "image/jpeg";
context.Response.WriteFile(imageName);
}

public bool IsReusable
{
get
{
return false;
}
}
}
}

The example above shows the use of a generic handler that handles the .ashx file extension. You can also server custom file extensions using almost the same code and adding new httpHandler entries to the web config file.

4 comments:


  1. Thanks for your marvelous posting! I quite enjoyed reading it, you happen to be a great author. I will remember to bookmark your blog and will eventually come back very soon. Go to best social plan for get more related topic. Have a nice evening!
    Situs BandarQ, Poker, Domino 99 Online Terpercaya - Situs Judi Bola Online Terbesar dan Terpercaya Indonesia

    ReplyDelete
  2. Telah Hadir Agen Poker, DominoQQ, Capsa Susun, Bandar Ceme,Ceme Keliling, & Live Poker Terbaik dan Terpercaya.

    Hanya dengan Minimal Deposit 10 ribu Bonus 10 ribu dan Minimal Withdraw 25 ribu anda dapat bermain di Acehpoker;

    1. Acehpoker memberikan bonus Referral 15% (Seumur Hidup)
    2. Sistem keamanan terjamin dan Data pribadi akan aman bersama kami.
    3. Player Vs Player ( 100% No Robot)
    4. Dengan Sistem Confirm Deposit terbaru Max 2 menit (Jika semua Nama di Id dan di bank
    sama)
    5. CS yang ramah dan Melayani dengan Professional
    6. Online 365 Hari 24 Jam
    7. Anda Menang berapapun Kami Bayar tanpa Maksimal Withdraw dan Tidak ada batas maksimal
    withdraw dalam 1 hari.
    8. Dapat bermain 6 permainan hanya dengan 1 Web , yaitu Poker , DominoQQ , Bandar Ceme, Capsa Susun, Ceme Keliling, Live Poker
    9. Bisa bermain di Android dan IOS
    10. Sistem pembagian Kartu menggunakan teknologi yang mutakhir dengan sistem Random Permanent (acak) , Dan tersedia Ratusan meja untuk tempat anda bermain

    Pin BBM : 563AB8CA
    yahoo : mahadewi374@yahoo.com
    Line: Lilly080615

    Bola
    Agen Bola Terpercaya
    Daftar Judi Bola Maxbet
    Bandar Bola IBCBET
    Daftar Bola BCA
    Cara Bermain Poker
    Agen Poker Terpercaya
    Poker Bonus Deposit
    Cara Daftar Poker
    Bandar Ceme Terpercaya
    Cara Bermain Poker
    Situs Poker Terpercaya
    Judi Bandar Capsa
    Poker Bonus Member Baru
    Panduan Bermain Poker
    Agen Poker Terbaik
    Poker Promo Poker Online
    Poker Online Terpercaya
    Capsa Susun Uang Asli
    Qiu Qiu Uang Asli
    Poker Online Terbaik
    Domino QQ Online
    Bandar Ceme Keliling
    Judi Poker Online
    Agen Ceme Keliling
    qiu qiu
    texas holdem poker
    Poker Online Indonesia

    ReplyDelete