Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpHandler 101 FAIL

When I add an HTTP handler:

<add verb="*" path="*test.aspx" type="Handler"/>

With the class:

using System;
using System.Web;

public class Handler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
    }

    public bool IsReusable
    {
        get { return false; }
    }

}

My ASP.NET application dies with the error "Could not load type 'Handler'." when I try to access http://localhost:port/mysite/this-is-a-test.aspx.

I thought maybe it was a namespace issue, so I tried what follows, but got the same "Could not load type 'Test.Handler'." error.

<add verb="*" path="*test.aspx" type="Test.Handler, Test"/>

With the class:

using System;
using System.Web;

namespace Test
{

    public class Handler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");
        }

        public bool IsReusable
        {
            get { return false; }
        }

    }

}

I knew I was getting rusty with ASP.NET, but I'm without a clue on this one.

like image 835
core Avatar asked Dec 18 '22 09:12

core


1 Answers

I guess you are using a web site project in contrast of web application project. In this case you need to put the code behind file of your handler (Handler.cs) in the special App_Code folder. The markup file (Handler.ashx) may be at the root of your site:

<%@ WebHandler Language="C#" Class="Handler" CodeBehind="Handler.cs" %>

Then you can directly declare your handler in web.config:

<add verb="*" path="*test.aspx" type="Handler"/>
like image 114
Darin Dimitrov Avatar answered Dec 22 '22 01:12

Darin Dimitrov