sylverfyre
sylverfyre

Reputation: 3129

ASHX including code-behind class -

I'm trying to use an external code file to include some helper classes for several .ashx handlers. The example one is using ListToJSON, which just turns nested Lists (Theres stuff like List<List<whatevers>> that are being worked with) into JSON (which will get thrown into context.response)

The ListToJSON class works fine when it's in the same file. I'm trying to put it into a Helper.cs file which is included in the same project in VS2010, because these classes get used in several different handlers.

I was under the impression that "using Helper;" was what I needed to do, but I still get the error "The type or namespace Helper could not be found (are you missing a directive or assembly reference?" (and intellisense doesn't see it either)

I've also tried putting both code files into the same namespace. Same error.

It's not a DLL file, it's just a c# code file in the same project. Should I realy be compiling it as a DLL to do this? If so, how do I do that? (Once I do, I can do the right click->add reference, correct?)

I think I'm supposed to be using the App_Code folder, but I'm not really sure how to set that up in VS so that it's referenced properly.

My handler file (skeleton)

<%@ WebHandler Language="C#" Class="generateReport" %>

using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Diagnostics;
using Helper;
public class generateReport : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        ListToJSON Converter = new ListToJSON();
        //context.Response stuff goes here.
    }
}

My helper file, Helper.cs - this is in the same project directory and is included in the project:

using <stuff>;
namespace Helper
{
    public class ListToJSON
    {
        //class definition here
    }
}

UPDATE: So, I put Helper.cs into the /App_Code/ folder, and it seemed to be playing nicely. Intellisense was picking up things in Helper.cs after I did using Helper; When I tried it on IIS, it I got the following familiar error:

Compiler Error Message: CS0246: The type or namespace name 'Helper' could not be found (are you missing a using directive or an assembly reference?)
Line 19: using Helper;
Source File: <path>\info.ashx    Line: 19

I get no errors when running this through visual studio's IIS emulator. When I run it through IIS (localhost) I get the IIS internal server error described. App_Code folder was made through VS, I right clicked and chose Add ASP.NET Folder > App Code\

Edit: Added tag iis

Upvotes: 0

Views: 8783

Answers (2)

sylverfyre
sylverfyre

Reputation: 3129

For IIS to read classes correctly from the App_Code folder, the App_Code folder (or the bin folder, for compiled assemblies) needs to be in the IIS root directory in order for classes within to be detected.

In my case, I was publishing to a subfolder of the IIS root directory, and so IIS couldn't see the classes in it.

Upvotes: 0

Pankaj
Pankaj

Reputation: 10105

I assume that you are using WebSite and not WebApplication.

While adding new Class file in the Website, It should be in the App_Code Folder to avoid the Accessibility issues.

Edit - 1 => Please see below the Publish details.

enter image description here

Edit = 2 => Try adding the individual Assembly in the bin folder as stated below. This will confirm that the App_Code folder Dll are incorporated.

enter image description here

Upvotes: 2

Related Questions