Step 1. Creating Handler Dll
Let’s create an http handler dll which we will later import into our project.
Create a Class Library Project add a class named HandlerClass to it and apply the interface IHttpHandler.
Now, let’s write some code for the ProcessRequest (implemented through the interface) method in this class.
public void ProcessRequest(HttpContext context)
{
if (context.Session["UserId"] == null)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Please Login to download this file");
}
else if (context.Session["UserId"] != null)
{
string strURL = context.Request.RawUrl;
context.Response.ContentType = "text/plain";
context.Response.Write(strURL);
// Get the file name
string strFileName = strURL.Substring(strURL.LastIndexOf("/") + 1);
string strFilePath = context.Server.MapPath(strFileName);
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.Clear();
response.AppendHeader("content-disposition", "attachment; filename=" + strFilePath);
response.ContentType = "application/octet-stream";
response.WriteFile(strFilePath);
response.Flush();
response.End();
}
}
The above method check that the Session["UserId"] is not null.
If the value is null then the user is not allowed to download the file, else the file is downloaded.
Now, build this code and after successful build, create another web project where we would use this dll.
Step 2. Creating Handler Testing Project.
Let's create another WebApplication by the name "TestingHandler", add the Reference of the above project into it.
After that, let's create a folder called files in the project and add certain files to it (pdf, zip, word etc).
I am currently including some random documents for the following formats: pdf, docx, rtf and zip
Now, I would register my http handler in this application and in order to do that, we would write the below code to the web.config file in the application.
The first set of lines would go under the system.web tag and it is as in the following code snippet.
<httpHandlers>
<add verb="*" path="*.pdf" type="MyHttpHandler.HandlerClass, MyHttpHandler"/>
<add verb="*" path="*.zip" type="MyHttpHandler.HandlerClass, MyHttpHandler"/>
<add verb="*" path="*.rtf" type="MyHttpHandler.HandlerClass, MyHttpHandler"/>
<add verb="*" path="*.docx" type="MyHttpHandler.HandlerClass, MyHttpHandler"/>
</httpHandlers>



Divi Perdomo AnchetaPosted Feb 5, 2020, 3:15 PM
Finally something that worked. I am only having issues with a subapplication on my IIS. It complains about missing assembly files. Any idea how can I fix this. I checked and the handler has been registered on the subappliaction as "inherited"
Shobana JPosted Jul 12, 2016, 3:37 AM
Nice share
Asfend YarPosted Feb 29, 2016, 2:09 PM
Thanks for valuable information
Sr KarthigaPosted Feb 26, 2016, 9:35 AM
good one
Sr KarthigaPosted Feb 26, 2016, 9:35 AM
nice explanation
pooja guptaPosted Oct 29, 2015, 9:55 AM
nice and helpful article
Akash MalhotraPosted Sep 28, 2015, 2:44 PM
Nice article
Pankaj GuptaPosted Sep 28, 2015, 1:34 AM
Good one..
Ankit BansalPosted Sep 28, 2015, 12:52 AM
nice one..thanks ..
Harshad PansuriyaPosted Sep 28, 2015, 12:39 AM
Nice One
Sujeet SumanPosted Sep 27, 2015, 9:42 AM
Nice Article...........
Anupam SinghPosted Sep 27, 2015, 8:39 AM
nice and simple demonstration.
Vipul MalhotraPosted Sep 27, 2015, 3:40 AM
Thanks Rakesh
RakeshPosted Sep 27, 2015, 12:38 AM
Good share sir
Praveen KumarPosted Sep 26, 2015, 10:57 PM
Very Nice