6. HttpModules
Simple classes that can plug
themselves into the request-
processing pipeline
Attaching to events thrown by the
application.
Similar to Global.asax
7. Global.asax HttpModules
Store global state Store global state
between requests between requests
Respond to application- Respond to application-
wide events wide events
A separate
Specific application
assembly, that can be
referenced by any
application
Implements generic
functionality that’s useful
between different
applications.
Global.asax vs. HttpModules
8. Creating an HTTPModule
Create a class implementing
System.Web.IHttpModule interface
Requires to implement : Init and
Dispose
◦ Init:
Input parameter: HttpApplication context
Access current application context
To implement HttpModule functionality
Used to wire-up different events that fire during
request processing
9. Creating an HTTPModule
◦ Dispose:
called once when the application terminates.
Application terminates and reinitializes when:
The application or IIS restarts,
The Web.config configuration file or a dependent
assembly is modified.
Configure module in web.config
14. ASP.NET features handled by
modules:
Output Caching
Session State
Windows Authentication
Forms Authentication
Passport Authentication
Role manager
Anonymous Identification
Profile
ErrorHandlerModule
15.
16. HttpHandlers
The last stop for incoming HTTP
Request
The point in the request processing
pipeline that is responsible for serving
up the requested content
Mapped to a specific file extension
(.aspx, .html,.jpg…………)
Unlike modules, only one handler is
used to process a request.
17. Built-in HTTP Handlers
Handler Description
ASP.NET page handler (*.aspx) The default HTTP handler for all
pages.
Web service handler (*.asmx) The default HTTP handler for Web
services
Generic Web handler (*.ashx) The default HTTP handler for all
Web handlers that do not have a
UI and include @WebHandler
directive
19. Generic HttpHandlers
Add new item Generic Handler
(*.ashx)
Class implements IHttpHandler
◦ ProcessRequest:
Actually process incoming HTTP request.
Input argument:
HTTPContext object has access to all objects of the
current HTTP context as
response, request, user, profile, ……..
◦ IsReusable:
Property to let ASP.NET know whether incoming
HTTP requests can reuse the same instance of the
HttpHandler
22. HttpHandlers to custom file
extensions
Create a class that inherit from
IHttpHandler, add it to App_code or in
a separate library project to be added
as an assembly
Implement the 2 methods same as
previously
Add configuration to web.config
28. Publish on Local IIS
Must run Visual Studio as
Administration
Install all needed IIS components
Reinstall ASP.NET if needed
Make sure application pool is set to
.NET Framework 4.0
Add user(with permissions to db and
files) to application pool identity
30. Important Note
To avoid problems with configuration
in machine root web.config
◦ Add <clear /> in all providers
To avoid problems with users and
roles in multiple applications add
applicationName attribute to all
providers
32. REFERENCES
[1] Beginning ASP.NET 4 In C# 2010, Matthew
Macdonald, Apress
[2] Web Application Architecture
Principles, Protocols And Practices, Leon Shklar
And Richard Rosen, Wiley
[3] Professional AS P.NE T 4 In C# And VB, Bill
Evjen, Scott Hanselman And Devin Rader, Wiley
[4] Pro ASP.NET In C# 2010, Fourth
Edition,matthew Macdonald, Adam Freeman, And
Mario Szpuszta, Apress
Editor's Notes
Successor of ISAPI extensions and filters.Interact with the Web server itself.Interact with low level processes, as the processing of incoming and outgoing HTTP requests.
Study ASP.NET request pipeline first
AppendMessage.cs
Difference between dynamic and static sites
Try changing ContentType
imageHandler.ashx
Note to add it to IIS it must be an assembly
Web.config: In all providers <clear/> first add applicationName