Andrius Naruševičius
Andrius Naruševičius

Reputation: 8578

Custom http handler in web.config

I have and IIS website which consists of just index.html and web.config. I want to override the request process based on certain conditions. Thus I found a tutorial and wrote my handler by creating a new Class Library:

namespace skmHttpHandlers
{
    public class SimpleHandler : IHttpHandler
    {
        public void ProcessRequest (HttpContext context)
        {
            context.Response.Write ("<html><body><h1>Test</h1></body></html>");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

I built the library and copied skmHttpHandlers.dll into the folder where index.html and web.config resides. Unfortunately it doesn't find my handler and shows an error:

Could not load file or assembly 'skmHttpHandlers' or one of its dependencies. The system cannot find the file specified.

My web.config looks like this:

<configuration>
    <system.webServer>
        <handlers>          
            <add name="SimpleHandler" path="*" verb="*" 
            type="skmHttpHandlers.SimpleHandler, skmHttpHandlers" 
            resourceType="Unspecified" 
            requireAccess="Script" 
            preCondition="integratedMode" />
        </handlers>
    </system.webServer>
</configuration>

What am I doing wrong?

Upvotes: 1

Views: 1450

Answers (1)

CodeFuller
CodeFuller

Reputation: 31282

IIS looks up handler assembly in predefined set of directories. The directory of Web application itself is not in the list, that's why you get FileNotFoundException. The suggested location for the handlers is bin directory in the application root. So to fix the problem - create bin directory near web.config and move skmHttpHandlers.dll assembly in it.

In the future, when you encounter similar problems, you could enable Fusion log to get detailed information where the assembly was looked up:

enter image description here

Upvotes: 1

Related Questions