user1068636 user1068636 - 1 month ago 7
ASP.NET (C#) Question

Why is my C# handler code not executing even though I copied the DLL file into the bin directory and modify web.config appropriately?

I am getting the following error:

enter image description here

I created a new project using the "WebSite" template and was successfully able to load the page in internet explorer 10 by accessing http://localhost:54530. Then I created another new project using the C# Class Library template which generates DLLs. Here is how my class library looks like:

using System;
using System.Web;


namespace WebRequestInterceptorModule
{
public class Class1 : IHttpHandler
{


public bool IsReusable
{
get
{
return false;
}
}

public void ProcessRequest(HttpContext context)
{
HttpRequest request = context.Request;
System.Collections.Specialized.NameValueCollection nvc = request.Params;

string test = nvc.Get(0);

// Example #4: Append new text to an existing file.
// The using statement automatically flushes AND CLOSES the stream and calls
// IDisposable.Dispose on the stream object.
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(@"C:\Users\Dining Room\Documents\WriteLines2.txt", true))
{
file.WriteLine(test);
}

}
}


}

After building the class library solution shown above, I get two files:

WebRequestInterceptorModule.dll
WebRequestInterceptorModule.pdb


I copy both files into WebSite project's bin directory :

copy WebRequestInterceptorModule.dll C:\Users\Dining Room\Documents\Visual Studio 2015\Projects\WebSite1\bin

copy WebRequestInterceptorModule.pdb C:\Users\Dining Room\Documents\Visual Studio 2015\Projects\WebSite1\bin


Last step, I modify web.config :

<system.web>
<httpHandlers>
<add verb="*" path="WebRequestInterceptorModule"
type="WebRequestInterceptorModule.Class1" />
</httpHandlers>
</system.web>


The problem is when I go back to http://localhost:54530 I do not see a new file written to C:\Users\Dining Room\Documents\WriteLines2.txt

What did I do wrong?

Rob Rob
Answer

You're not using the path directive correctly.

<add verb="*" path="(someURL)"
       type="WebRequestInterceptorModule.Class1, WebRequestInterceptorModule" />

You use the type field to indicate the class AND the .dll. The path directive tells ASP.NET which URL binding to use.

See more here: http://www.hanselman.com/blog/ConditionallyServeFilesStaticallyOrDynamicallyWithHttpHandlers.aspx

Comments