Why is better (or not) to use an httphandler?

T

the4man

Hi all!

I have an app that show images that are stored in SQL Server. To show
the images on screen, until now (with the "old" ASP), I use the
following code:

<img src="showimage.asp?id=20" />

And in "showimage.asp" (more or less, I write from memory)

Response.Contentype ="binary"
.....
....get the image from DB
.....
Response.BinaryWrite imagecontent.

Now, with ASP.NET I have the posibility of use a httphandler to do
this. For example, I could use <img src="40.gfx" />, with a httphandler
for the .gfx extension that generate the image (as in the old
showimage.asp page).

I think that the second method is more elegant. Even mor efficient (it
consume less resources because the httphandler class is lighter than
page class). But, besides that, is there any other reason to prefer the
second method instead of the first one?

Thanks in advance!
 
K

Karl Seguin [MVP]

there are two main reasons I can think of...first, it's more efficient as
you say...secondly, it's far more portable. That handler is in a nice little
assembly that can be reused, far more easily, than your aspx file.

Karl
 
K

Kevin Spencer

Generic handlers do not create a session object.
One consideration...

No HttpHandler (including System.Web.UI.Page) creates a Session. The Session
is part of the HttpContext passed to the HttpHandler (all HttpHandlers) with
the Request, in the ProcessRequest method.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A brute awe as you,
a Metallic hag entity, eat us.
 
K

Karl Seguin [MVP]

Doesn't the handler need to be marked with IRequireSessionState or the
readonly version in order for sessions to be available? I always assumed
sessions must be so expensive to create, the made you explicitly ask for
them to be available (relatively speaking of course).

Karl
 
K

Kevin Spencer

Hi Karl,

I suppose it depends on how you look at it. The HttpContext has Session, but
in order to read/write it you do have to implement either
IRequiresSessionState or IReadOnlySessionState. The interface has no
members, but is only a marker.

My point was that the Session is not created by the Handler, but is passed
off to it via the HttpContext. But it's a good thing you brought that up in
order to prevent any confusion!

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A brute awe as you,
a Metallic hag entity, eat us.


"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:[email protected]...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top