When exactly to use HttpHandlers and HttpModules?
Can't I write that code in ASPX pages' code behind?
HTTPHandlers are used by ASP.NET web application server to handle specific requests based on extensions. HTTPHandlers run as processes in response to a request made to the ASP.NET website. It is a class that implements the System.
Both Middleware and HttpModule are pieces of code blocks which lets two or more components interact together during a request execution. HttpModules are registered in the web. config file of the ASP.NET framework while a Middleware is registered via code inside an ASP.NET Core application.
To explain HTTP Modules and HTTP Handlers, HTTP module and HTTP handler are used by MVC to inject pre-processing logic in the request chain. HTTP Handlers are extension based pre-processor whereas HTTP Module are event based preprocessor.
HttpModules helps you to attach code specific to a application events. HttpModules are tied to System. web. Middleware are configured in Startup.cs code rather than web.config file (entry point for application) Unlike HttpModules, there is full control of what get's executed and in what order.
HttpModule allows you to intercept the request (before it is handled by its handler) and response generated. It can modify both request/response if needed. ASP.NET sessions, profiles, authentication etc is also implemented as HttpModule - these module inspects the request and attach necessary context (e.g. session state based on session cookie) to the request. Such functionality is difficult to achieve via aspx code behind.
HttpHandler is the one responsible for handling the request i.e. to generate HTTP response which often (but not always) means generating some html. The page class (base for aspx) is nothing but http handler (so are other end points such as asmx, ashx, axd). The raw handler (ashx) is useful when you need absolute control over response generation - it would be possible to use aspx instead but then it would unnecessarily involve all default page/control machinery (view-state, post-date, control tree etc).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With