HttpModules Simple classes that can plug themselves into the request- processing pipeline Attaching to events thrown by the application. Similar to Global.asax
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
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
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
ASP.NET features handled bymodules: Output Caching Session State Windows Authentication Forms Authentication Passport Authentication Role manager Anonymous Identification Profile ErrorHandlerModule
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.
Built-in HTTP HandlersHandler DescriptionASP.NET page handler (*.aspx) The default HTTP handler for all pages.Web service handler (*.asmx) The default HTTP handler for Web servicesGeneric Web handler (*.ashx) The default HTTP handler for all Web handlers that do not have a UI and include @WebHandler directive
Creating HttpHandlers Using generic handler .ashx Mapping the HttpHandler to a custom file extension.
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
HttpHandlers to custom fileextensions 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
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
Publish on Local IIS Use Copy Website Use Publish Website
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
Reading Assignment #8 Chapter 26 – Professional ASP.NET 4 in CS and VB
REFERENCES  Beginning ASP.NET 4 In C# 2010, Matthew Macdonald, Apress  Web Application Architecture Principles, Protocols And Practices, Leon Shklar And Richard Rosen, Wiley  Professional AS P.NE T 4 In C# And VB, Bill Evjen, Scott Hanselman And Devin Rader, Wiley  Pro ASP.NET In C# 2010, Fourth Edition,matthew Macdonald, Adam Freeman, And Mario Szpuszta, Apress