asp.netnamespacesapp-code

asp.net web sites and default namespaces and LINQ Datacontext part 2


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.


Solution

  • 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.