views:

1566

answers:

6

Let's say I have a class (MyClass.cs) in App_Code (using a ASP.NET web site, not project). There's no namespace assigned to the class.

How can I get that class to be valid in the web site .aspx pages? WHAT do I need to put in the web.config file and WHERE do I need to put it?

Do I <add assembly tag or do I <add type ??

Since the assembly tag requires version, culture, and public key, I'm not sure what those values are at compile time.

Do I just add a type tag? Where do I put it in the web.config?

EDIT: Ok, I found part of my answer. I was getting the error because "http://localhost/MyFolder" was not set as an "application" in IIS. However, I have a BUNCH of folders, "http://localhost/MyFolder2, http://localhost/MyFolder3, etc...

New question: Is there any way to NOT have MyFolder be an application, and still make it run correctly? I've heard of a "codesubdirectories" tag, is that useful and where would I put it? Thanks.

+1  A: 

In a web site, no name spaces are needed within your App_code folder. Nothing needs to go into your web.config to reference classes in your app_code folder.

Andrew Robinson
+1  A: 

I don't think you have to do anything at all. That's how web site "projects" work. Anything in App_Code gets compiled.

John Saunders
+2  A: 

This is one of those poorly documented aspects of dynamic compilation that cropped up when asp.net 2.0 web sites were first introduced.

An actual answer for your specific question though will require that you tell us more about what exactly you are trying to do with the class from App_Code. Most of the time you just don't have to worry about the namespace or assembly name to use those classes in your pages. You just use the class name and the compiler will figure it out and hook everything up for you.

The biggest exceptions I've run into with this are when using web controls that I've put into app_code. For those you need a @Register directive in the aspx page... and for that you need an assembly name and a namespace.

The files in app_code are compiled into an assembly named "__code" (note that this has TWO underscores, not one). That's what you can use when you need the assembly name for Register directives or in web.config or what not.

But, as far as I know, you will not be unable to use a class in register directives or some web.config settings that require a namespace unless you have explicitly wrapped that class in a namespace block.

Stephen M. Redd
A: 

Check to make sure the class is Public, maybe?

Jeremy
A: 

Going by your edit, have you tried moving the App_Code folder to the root of the site?

/App_Code
/MyFolder
/MyFolder2
/MyFolder3

That may address your issue.

You could also take a look at Scott Guthrie's Tip/Trick:

Creating Sub Web Projects using the VS 2005 Web Applications

Although this does use the Web Application projects, rather than web sites.

Looking at the CodeSubDirectories config element - you probably could use this - it would need to be defined in the root web.config I guess.

Also, note that the <assembly> references only need to contain version, culture and public key details if the assemblies are strongly named (and so have those values).

Zhaph - Ben Duguid
+1  A: 

Check the properties of the code file in Solution Explorer.

I had a situation where one of the .cs code files had "Content" selected for the Build Action. All the other files in App_Code were compiling fine but not this one.

Changed the Build Action to "Compile" and it started working as expected.

Bing