App_Code and web.config - asp.net

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?

Related

ASP.net Web Site referencing code files with dependencies on non-compiled code

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.

Cannot Parse .aspx during WAP Conversion

I've copied over the Web Site files to a new WAP project. Made sure that it compiles and it does. The problem I have is that it can't parse the .aspx pages when I perform a convert to WAP. The error simply says it can't parse the .aspx pages.
When I look at one of the .aspx pages, I have the page directive as such but it can't recognize that namespace. That is the namespace I'm using for my new WAP project.
My code-behind pages all have the namespace Instant.Web as well as the project properties is set to Instant.Web for the primary namespace.
so when I go to a page for example, it's not recognizing the Web.Attachment portion of my Inherits.
Here's an example (slightly modified the wording for privacy):
SomeAspxPage.aspx:
<%# Page Language="C#" Inherits="Instant.Web.Attachment" Codebehind="SomeAspxPage.aspx.cs" %>
<asp:Content ID="ctlContent" ContentPlaceHolderID="ctlContentPlaceHolder" runat="Server">
<Frm:Attachment ID="Attachment1" runat="server"/>
</asp:Content>
SomeaspxPage.aspx.cs
namespace Instant.Web
{
public partial class Attachment : Instant.InstantFrm.Pages.Base.UserPage
{
}
}
Also for some reason UserPage in the code-behind isn't showing as a class that you can right-click and go to source. Not sure why because that assembly is definitely in this WAP Project and it's the latest assembly for Instant.InstantFrm
Do not know if it is relevant for your real case, but as for your example, you have an attribute Codebehind="Attachment.aspx.cs" which looks for the file Attachment.aspx.cs, but the file's name is SomeaspxPage.aspx.cs. Usually "file could not be parsed" means the wizard cannot find the matching code-behind file and cannot convert the page.
BTW one more note from here:
Before converting the project files, add references to the Web
application project for assemblies that existed in the Bin folder to
help prevent errors.
It is not clear from your description if you have added the reference to your custom UserPage. If not and that assembly is just copied somewhere into WAP Project it can also cause the converting error.
Try adding a new blank page to your project, check if that can be parsed.
Then in small steps add functionality until you get the page that is the same as the one that is giving you the error.
This should allow you to identify what the problem is.
Based on this quote Warning 126 Unrecognized tag prefix or device filter 'asp' in one of the comments, it sounds like you are missing an Assembly reference in your CSProj file.
Verify that you have System.Web in your References folder of the project.
If it is there, verify the version (directory) it is pulling from is the same as what .NET version you are targeting with your project.
Save and close the solution, then re-open it and try again.
I'm theorizing that VS is caching something and the projects needs to be reloaded.
For WAP site, I'd remove the ASPX layout completely (except the first line, which is required) and output the content using Response.Write in codebehind.
In addition to simplicity, this gives much cleaner output and granular control over the process.

consuming classes from folder other than App_Code

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?

Not able to create an instance of a class

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.

CodeBehind class not accessible to code in App_Code folder

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

Resources