I have a PlannerShiftView user control in the root folder of my ASP.NET web site. In my App_Code folder, I have a ShiftViewTemplate class that needs to instantiate a PlannerShiftView (for a TemplateField in a GridView). The problem is, the following code doesn't compile because the PlannerShiftView type is not available in what I deem to be the App_Code 'phantom namespace'.
Please can somebody explain to be what is happening here, as well as what to do. I know I can just move the ShiftViewTemplate out of App_Code, going against convention and without explantion, but that is something of a hollow victory.
ASP.Net websites do not compile into a single assembly, but into several ones. Therefore the code of your user control in not referenceable by the App_Code assembly.
The way I solved a similar case:
declare an IPlannerShiftView interface in App_Code or an external assembly
declare the PlannerShiftView control as implementing the IPlannerShiftView interface
instantiate the control using Page.LoadControl, and cast to interface
Related
I have a website that is not utilizing the App_Code folder for jit compiling code files (that is what App_code is for, right?). Instead developers have to either cram all the necessary classes that they need for a page into the code file, or into an individual .cs file that gets and Assembly Directive src on the page (this is because of dependencies between the source files).
Is there any way for classes with dependencies on each other to be placed in separate files?
For instance Presenter.cs has a dependency on Model.cs. I would like to just add an assembly directive to my page for Presenter.cs, and make it happy. But since this does not compile Model.cs, it doesn't work. Adding Assembly references for both code files also does not work.
Is there a mechanism that I am missing?
EDIT: I think the answer is that the App_Code folder is exactly created for this task and that there is no other/better way to do it.
When you say "CodeBehind" are you actually referring to "CodeFile"? (One of the many reasons I'm not a fan of "ASP.NET Websites" and prefer MVC).
AFAIK, the ASP.NET runtime compiles all of the App_Code files together into their own assembly, however code in each page's CodeFile (not their CodeBehind) can only reference this anonymous assembly and not each others' code.
Can you clarify what you mean by "page assembly reference"? Are you doing <%# Assembly %> or do you mean a namespace import <%# Import %> (because as I said, a lot of the code is compiled into an anonymous assembly).
I suggest you just move the common code and classes to *.cs files within App_Code. It should just work.
I am creating a site wherein I have a folder which contains some .cs files. I want to access those classes in .aspx and .ascx files. I’ve created some properties in it, but when I create the object of the class I don’t find that property via IntelliSense.
How can I use and consume those properties from that .cs file?
The website will only compile code files that are in the App_Code folder or are codebehind files for referenced controls. There isn't a way to reference classes defined in code files outside of the App_Code folder.
If you compile those classes and put the resulting dll in your website's bin folder, then you can reference them. To do that, you'll need to add them to a Web Application project in Visual Studio. See Ian Robinson's WAP blog post for most details.
Just have those properties public and you'll see them.
If still no luck please post your code and also tell: can you create instance of the class without error? Can you access any other properties or methods?
In a Project I have different modules or folders.In a module I have a namespace called idsobject.I have class in this namespace.
In another CS fle in the same folder I'm trying to access this class.But i'm not able.while if put that cs file in aPP_code than i able to get that class.
how to access the class in another cs file within the same folder.
thanks in advance
In an ASP.NET web site project, *.cs files are only compiled and made available to the rest of the application if they're located in the App_Code folder, or a subfolder in App_Code, or if they are associated with a *.aspx as code behind. The architecture is intended to allow full server-side compilation. You can't just put a *.cs anywhere and expect IIS to be able to find it, compile it, and link it with the rest of the app.
With an ASP.NET web application, you can put *.cs files where ever you want, because they are compiled by Visual Studio using details that are kept in the project file.
I'm guessing that you're using a web site project, which is why it works when you put the file in App_Code.
First check the namespace. If you accessing the correct namespace then perhaps the class access is set to private.
You could give us some extra information.
This sounds strange. Putting a class in a file in a Web project with a .cs extension should work just fine no matter which folder the file is located.
The only thing I can suggest is to make sure that there is at least a
using idsobject;
line at the top of the code unit where you are trying to access the class. The default behaviour for ASP.NET Web forms is for no namespace to be defined, which can sometimes lead to confusion.
The class should be marked as public if you've tried to access it from another namespace.
If it doesn't help, please post here a problematic code snippet.
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.
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.
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.
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.
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).
I don't think you have to do anything at all. That's how web site "projects" work. Anything in App_Code gets compiled.
Check to make sure the class is Public, maybe?
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.
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.