In this section, we will continue from the previous point and delve further into MVC components. Here, in this case we will look at building custom handler. HTTPHandlers implement IHTTPHandler and generate a response to HTTP-Request. Therefore, we first need to create the Handler and then register the same with the application either through code or via config file. Now, IHTTPHandler exposes two properties.
- IsReusable:- IsReusable is just a flag which says that whether the handler can be reused or not across requests.
- ProcessRequest():- ProcessRequest is the main execution method of the class which implements IHTTPHandler and also this is the one responsible for generating the response. Also, one important point to note that a handler can request to almost anything like plain-text, file, JSON etc. Now, without wasting time let us get started.
Now, let us look @ the demo quickly.
Here, I have simply created one handler and implemented the interface. Currently, I have pasted the code in it’s raw format. Now, let me go ahead and modify the snippet.
Next, we need to go ahead and register this handler. Here, I will do the same from the code as shown below in the snippet.
With the above change in place, when I run the app, then it will produce the home page as expected. But when, I click on Home link it will get redirected to my blog as shown below in the screenshot. Also, notice the series of events happening here.
Therefore, the basic difference between HTTPHandlers and HTTPModule is there can be as many modules serving one HTTP-Request. However, there is only one handler which will process the request and generate the HTTP-Response. Also, Handlers are implemented using IHTTPHandler and modules by IHTTPModule. I hope you would have liked this discussion. I hope, you would have liked this discussion. We will delve further in coming Sections.
Code Download Link:- https://github.com/rahulsahay19/MVCLifeCycle