Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / April 2008

Tip: Looking for answers? Try searching our database.

HttpHandler

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David - 16 Apr 2008 15:38 GMT
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
Mick Wilson - 16 Apr 2008 15:52 GMT
> 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/CompositingTwoImagesIntoOneFromTheASPNETServerSide.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.
Alexey Smirnov - 16 Apr 2008 16:02 GMT
> 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.
David - 16 Apr 2008 16:24 GMT
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/CompositingTwoImagesIntoOneFromTheASPNETServerSide.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

>> 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
[quoted text clipped - 4 lines]
> job but cannot be called from the URL, you have to configure it in
> IIS.
George Ter-Saakov - 16 Apr 2008 16:49 GMT
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.NETPageLifeCycle03092005001
005AM/ASP.NETPageLifeCycle.aspx?ArticleID=600150a4-56e8-4f38-bfc0-319a85b81a90


George.

> Thanks Alexey,
>
[quoted text clipped - 22 lines]
>> job but cannot be called from the URL, you have to configure it in
>> IIS.
George Ter-Saakov - 16 Apr 2008 16:26 GMT
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 <img src="myimage.axd?imageid=1">

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.

> Hi,
>
[quoted text clipped - 15 lines]
>
> David
David - 16 Apr 2008 16:35 GMT
Thanks George - that explains it very well indeed.

David

> I had pretty much same question a while back... Here is my answer (to
> myself).
[quoted text clipped - 60 lines]
>>
>> David

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.