views:

1395

answers:

1

Let me try to ask this question from a different angle.

I noticed that everytime an aspx page gets rendered in the browser using the "web site" model, a random assembly gets created 'on-the-fly' in the Temporary ASP.NET files. Analyzing the assembly in Reflector shows that the class created for any given .aspx file is under the "ASP" namespace.

So, starting with a empty "Temporary ASP.NET Files" directory, I opened my ASP.NET "website" in VS2008, and launched the default page. Immediately I observed that a random directory was generated inside that folder. Working my way down the path, I found 2 DLLs created: App_Code.1lywsqqz.dll, and App_Web_iohekame.dll. I assume that all the .aspx pages in the website get compiled into App_Web dll and everything in App_Code folder gets compiled into App_Code.dll.

So if my App_Code C#/VB.net files are under the "ASP" namespace, and my App_Web files are created under the "ASP" namespace, how come I still get an error "Could not load type 'ASP.NothwindDataContext'?

Somebody said "you don't need namespaces in the App_Code folder", but I tried it without and still get "Could not load type 'NorthwindDataContext'".

So what's going on between the App_Code folder, the rest of the site, and namespaces?

EDIT: Here's my LinqDataSource in my .aspx file:

<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
 ContextTypeName="NothwindDataContext" EnableUpdate="True" 
 TableName="Categories">
</asp:LinqDataSource>

Neither "NorthwindDataContext", nor "ASP.NorthwindDataContext" works.

+2  A: 

Types in App_Code C# source files, just like any C# file, will not be put in a specific namespace unless specifically declared by namespace Name {...} around it. So a class MyClass declared in App_Code will have the fully qualified type name MyClass. Just that.

You can reference it in Web.config as: "MyClass, App_Code".

Side note: When you are using a DBML in App_Code, the namespace of generated classes are defined in that file (look at the properties window when DBML file is open). If you specify a namespace in that file, naturally, your classes will be defined in that namespace. Note that this does not contradict with what I said above. The thing is, the LINQ data context generator processes the file and defines the classes in the specific namespace.

Mehrdad Afshari
So, are you supposed to have an entry in Web.config for EVERY class in App_Code?Can you give me a complete example web.config example? thanks.
Robert
Nope! Not at all. I just said that *in case you need it*.
Mehrdad Afshari
Also, do I need any "#imports ...." statement in my .aspx?
Robert
No. You don't. Since it's not declared in a namespace. The full name is "MyClass". Just like you can use `System.Console` without an Imports (since you are saying its full name), you can use `MyClass` too, since that's its *complete* name.
Mehrdad Afshari
Can you elaborate on your 'Web.config as "MyClass, App_Code" comment above?Which tag do you use? Where do you put it?<system.web>? The web.config is tag-soup to me.
Robert