HttpHandler

HttpHandler

am 16.04.2008 16:38:20 von Pietje puk

Hi,

I'm very new to ASP.NET. I have over 10 years of C++ experience in MFC
and Games programming etc, but web programming is something I have just
started.

Sorry for the rather silly question, but can someone explain what a
HTTPHandler is for?

I gather they can handle custom file extensions, but they seem to be
used to handle other events (such as authentification).

In my normal programming practise I would create a C++ class (or perhaps
in this case a C# class) to handle any logic, so what specifically would
a HTTPHandler or a HTTPModule do that a class couldn't?

Thanks

David

Re: HttpHandler

am 16.04.2008 16:52:56 von Mick Wilson

> In my normal programming practise I would create a C++ class (or perhaps
> in this case a C# class) to handle any logic, so what specifically would
> a HTTPHandler or a HTTPModule do that a class couldn't?

An HTTPHandler is a class that implements the IHTTPHandler interface,
which is mainly the one method:

void ProcessRequest(
HttpContext context
)

An HTTPHandler gives you access to the request earlier in its
lifetime, which can be useful for simple tasks.

Scott Hanselman's article shows you a pretty quick example here:

http://www.hanselman.com/blog/CompositingTwoImagesIntoOneFro mTheASPNETServerSide.aspx

One of the uses he found for HTTPHandlers was presenting an image of a
check to the user. No need for generating a control tree, events,
etc., so it made sense to handle it in a simpler way.

Hope this helps.

Re: HttpHandler

am 16.04.2008 17:02:22 von lexa

On Apr 16, 4:38=A0pm, David wrote:
> In my normal programming practise I would create a C++ class (or perhaps
> in this case a C# class) to handle any logic, so what specifically would
> a HTTPHandler or a HTTPModule do that a class couldn't?

HTTPHandler can be called directly from the browser by using its file
name in the URL. In C++ you can create an ISAPI that could do the same
job but cannot be called from the URL, you have to configure it in
IIS.

Re: HttpHandler

am 16.04.2008 17:24:27 von Pietje puk

Thanks Alexey,

I think I get what you are saying (Mick too in his post). One question
that remains is that some code or logic called be called in the
'Page_Load' method of an aspx page. So in the example that Mick gave,
the check image
(http://www.hanselman.com/blog/CompositingTwoImagesIntoOneFr omTheASPNETServerSide.aspx)
I can't see why that couldn't be done in a C# class (called in the
'Page_Load' method for example)?

Is it that it is just easier in a HTTPHandler? Obviously it's there for
a good reason! Perhaps my inexperience in web programming is getting in
the way of understanding this.

Thanks,

David

Alexey Smirnov wrote:
> On Apr 16, 4:38 pm, David wrote:
>> In my normal programming practise I would create a C++ class (or perhaps
>> in this case a C# class) to handle any logic, so what specifically would
>> a HTTPHandler or a HTTPModule do that a class couldn't?
>
> HTTPHandler can be called directly from the browser by using its file
> name in the URL. In C++ you can create an ISAPI that could do the same
> job but cannot be called from the URL, you have to configure it in
> IIS.

Re: HttpHandler

am 16.04.2008 17:26:15 von George Ter-Saakov

I had pretty much same question a while back... Here is my answer (to
myself).

You have 2 ways to process browser request...
1. Write ASPX page
2. Write HTTP Handler.

HTTP handler is low level and usually not used to produce HTML.
ASPX page is a high level and mostly used to generate HTML page.

As a matter of fact ASPX page processing is implemented using HTTP handler.
People from Microsoft wrote an HTTP handler to find aspx page, create
WebControls, run it... send output to browser...

There is nothing you can not do with HTTP handler that you can not do with
ASPX page.. You just going to waste some runtime if you do not need services
provided by ASPX.

So here is use-case scenarios so you would get better understanding.

1. Create HTML page that has couple buttons and grid on it.. ----- Use ASPX
page.. It's easy to manipulate those Web controls ASPX page creates for
you...

2. Get an image from the database using imageId ---- Use HTTP handler.
You do not need ASPX page... since you do not have webcontrols..
Just write an myimage.axd with will interpret parameter ImageId, get binary
data and send to browser using Response.BinaryWrite... call it in your HTML
pages

Of course you could have written aspx page in case 2 but you would waste
some run time..
Or you could have written a HTTP handler in case 1 but it would remind old
asp style of writing application.

------------------------------------------------------------ ---------------
HTTPModule is totally different and allows you to tap into request before or
after page is processed...
Use-case scenarios...
1. Url rewrite
2. Security like checking that user have used SSL where needed..
3. IP banning...

George.


"David" wrote in message
news:sq2dnd1OLOzCkpvVnZ2dnUVZ8vydnZ2d@brightview.com...
> Hi,
>
> I'm very new to ASP.NET. I have over 10 years of C++ experience in MFC
> and Games programming etc, but web programming is something I have just
> started.
>
> Sorry for the rather silly question, but can someone explain what a
> HTTPHandler is for?
>
> I gather they can handle custom file extensions, but they seem to be used
> to handle other events (such as authentification).
>
> In my normal programming practise I would create a C++ class (or perhaps
> in this case a C# class) to handle any logic, so what specifically would a
> HTTPHandler or a HTTPModule do that a class couldn't?
>
> Thanks
>
> David

Re: HttpHandler

am 16.04.2008 17:35:52 von Pietje puk

Thanks George - that explains it very well indeed.

David

George Ter-Saakov wrote:
> I had pretty much same question a while back... Here is my answer (to
> myself).
>
> You have 2 ways to process browser request...
> 1. Write ASPX page
> 2. Write HTTP Handler.
>
> HTTP handler is low level and usually not used to produce HTML.
> ASPX page is a high level and mostly used to generate HTML page.
>
> As a matter of fact ASPX page processing is implemented using HTTP handler.
> People from Microsoft wrote an HTTP handler to find aspx page, create
> WebControls, run it... send output to browser...
>
> There is nothing you can not do with HTTP handler that you can not do with
> ASPX page.. You just going to waste some runtime if you do not need services
> provided by ASPX.
>
> So here is use-case scenarios so you would get better understanding.
>
> 1. Create HTML page that has couple buttons and grid on it.. ----- Use ASPX
> page.. It's easy to manipulate those Web controls ASPX page creates for
> you...
>
> 2. Get an image from the database using imageId ---- Use HTTP handler.
> You do not need ASPX page... since you do not have webcontrols..
> Just write an myimage.axd with will interpret parameter ImageId, get binary
> data and send to browser using Response.BinaryWrite... call it in your HTML
> pages
>
> Of course you could have written aspx page in case 2 but you would waste
> some run time..
> Or you could have written a HTTP handler in case 1 but it would remind old
> asp style of writing application.
>
> ------------------------------------------------------------ ---------------
> HTTPModule is totally different and allows you to tap into request before or
> after page is processed...
> Use-case scenarios...
> 1. Url rewrite
> 2. Security like checking that user have used SSL where needed..
> 3. IP banning...
>
> George.
>
>
> "David" wrote in message
> news:sq2dnd1OLOzCkpvVnZ2dnUVZ8vydnZ2d@brightview.com...
>> Hi,
>>
>> I'm very new to ASP.NET. I have over 10 years of C++ experience in MFC
>> and Games programming etc, but web programming is something I have just
>> started.
>>
>> Sorry for the rather silly question, but can someone explain what a
>> HTTPHandler is for?
>>
>> I gather they can handle custom file extensions, but they seem to be used
>> to handle other events (such as authentification).
>>
>> In my normal programming practise I would create a C++ class (or perhaps
>> in this case a C# class) to handle any logic, so what specifically would a
>> HTTPHandler or a HTTPModule do that a class couldn't?
>>
>> Thanks
>>
>> David
>
>

Re: HttpHandler

am 16.04.2008 17:49:41 von George Ter-Saakov

You are absolutely right..
It could have been done in Page_Load event....
But I hope you know an answer already (still going to repeat it).

You would waste some CPU runtime by asking (by doing it as an aspx page) to
provide all services ASPX page providing and not utilizing them..

ASPX page have a so called "Page cycle" which is a sequence of bunch of
methods called at certain time.You do not need all that here... Hence a
HTTPhandler....

PS: My advice to get familiar with "page cycle" google it or here
http://www.c-sharpcorner.com/UploadFile/ShanthiM/ASP.NETPage LifeCycle03092005001005AM/ASP.NETPageLifeCycle.aspx?ArticleI D=600150a4-56e8-4f38-bfc0-319a85b81a90

George.

"David" wrote in message
news:zaidne8Zg_2zh5vVnZ2dnUVZ8smgnZ2d@brightview.com...
> Thanks Alexey,
>
> I think I get what you are saying (Mick too in his post). One question
> that remains is that some code or logic called be called in the
> 'Page_Load' method of an aspx page. So in the example that Mick gave, the
> check image
> (http://www.hanselman.com/blog/CompositingTwoImagesIntoOneFr omTheASPNETServerSide.aspx)
> I can't see why that couldn't be done in a C# class (called in the
> 'Page_Load' method for example)?
>
> Is it that it is just easier in a HTTPHandler? Obviously it's there for a
> good reason! Perhaps my inexperience in web programming is getting in the
> way of understanding this.
>
> Thanks,
>
> David
>
> Alexey Smirnov wrote:
>> On Apr 16, 4:38 pm, David wrote:
>>> In my normal programming practise I would create a C++ class (or perhaps
>>> in this case a C# class) to handle any logic, so what specifically would
>>> a HTTPHandler or a HTTPModule do that a class couldn't?
>>
>> HTTPHandler can be called directly from the browser by using its file
>> name in the URL. In C++ you can create an ISAPI that could do the same
>> job but cannot be called from the URL, you have to configure it in
>> IIS.