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

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.

Related

How asp.net application works?

I am quite new to .NET development and I am just wondering how does it work?
My undermentioned points are:
While developing ASP.NET application, under the project we have files like:
pagename.aspx
pagename.aspx.cs
pagename.asp.desiger.cs
After adding certain functionality to pagename.aspx page, assuming I have the development required web application (this is not my concern, what is developed)
Now I'm going to deploy this application, I use web deployment MSI which creates the required files in the one folder called folderdelopyed.
This folder contains the files required to support this application but interesting does not contain pagename.aspx.cs and pagename.aspx.designer.cs files.
My question is if folderdelopyed does not contain .cs file, then how does it work to run the segment of code which I have written in this file called PageName.aspx.cs?
The code in your cs files gets compiled into a dll.
For Web Application projects this is one dll
For Web Site projects, this is a dll per page.
All of the code is now in the dll's in the bin folder of the website.
You can use a tool like ILSpy (http://wiki.sharpdevelop.net/ILSpy.ashx) to look inside the dll's and see your code.
In the old days, for classic ASP, the script used to be embedded in your page - a mix of code and HTML, and was interpreted at runtime.
I like the new way more :-)
ASP.NET code is compiled into Dynamic-link library files, also known as DLL files.
The code you write in your code behind, which is the files with .cs extension, is compiled and put into whole new file, with .dll extension - and that file is copied to the server, to the BIN folder of your site.
Depending on what project type you choose, it's possible to have several DLL files for the web application, changing in every build - see dash's answer for more details.
On every .aspx page you have referece to what DLL file to use, as the very first line. For example:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="pagename.aspx.cs" Inherits="MyNameSpace.pagename" %>
In this example, the Inherits part determines what DLL to use. How? By the namespace, which is also the name of the DLL file.
When the above .aspx is requested by a browser, the .NET engine will go to the BIN folder, look for MyNameSpace.dll and in there look for class called pagename that inherits from the base Page class - all the rest is typical life cycle of ASP.NET page.
let me to say you something more Amazing.
you can hide your aspx file too.and put their content in to dll as same as your cs file put in dll.
you can make k aspx that just contain an address to the ddl file and no html body :D
that was greate!!! not only you can hide your cs file, you can hide you aspx file too :D

How to remove namespace from Inherits Attribute without a Parser Error, Could not load type

I've seen lots of posts about the Inherits Attribute, and the Parser Error "Could not load type"
I can get this working by putting "RootNamespace.PageName" for a specific page, where RootNamespace matches the Root namespace in my project properites.
But I would rather not put the namespace in there. i.e. I would rather put "PageName" than "Namespace.PageName".
I have a library project with a few DLLs and 10 or so .aspx and .ascx files.
To get an update of my library project, other projects in my company copy the DLLs in and then copy the .aspx and .ascx files into a specific folder in their project.
Only problem is every time they copy they have to change the Namespace of the inherits attribute to match the root namesapce in their project.
If they don't do this, they get no compiler errors but just get a Parser error when they hit the libary .aspx and .ascx files.
This is very annoying, it seems very ridiculous that so many pages will not work if the project root namespace changes.
Does anybody have any ideas on how I can make library pages and user controls for nuse withing other peoples projects?
Thanks,
Mike G
Ah ha! A colleague stumbled upon a way around this by accident...
OK I have a single shared "Library" project and many "normal" projects that make use of shared stuff from the Library...
1- Create a "Library" WebApplication that outputs a DLL, and put your web library code and also .ascxs and .aspx pages into the WebApplication project. 2- Reference the "Library" DLL in your "Normal" projects 3- Copy just the shared .aspx and .ascx files from "library" into the "normal" projects, but ... (important bit!) ... without the code behind
In our example we don't actually include the copied .ascx and .aspx files in the project (e.g. They're not referenced in the .vbproj file) and they don't get put in source control, they just get copied in from the library every time you build. We haven't experimented with what happens if you tell the project about the .aspx and .ascx files but they definitely load OK at run time.
So it does actually make sense no I think ab out it.
Basically the root namespace of the .aspx files is unachanged it's just refers to classes in the referenced library DLL so it all works.

Where can I put my source files?

I am developing a web form using Visual Web Developer
Currently I have class source files in the same directory as the web form, or in the App_Code folder. I'd like to organise the files into folders within the web form folder, but I can't find a way of adding a reference to the folders, and they don't seem to be being picked up automatically.
These are files that are under constant development.
What is the asp.net/c#'s equivalent concept to c++'s #include?
This is a Web Site rather than a Web Application
I'd suggest taking these out into a separate class library project and you can then reference this DLL in your web project. You would then add a 'using' statement at the top of your web form code to include this reference.
In a C# file (foo.cs), you would use:
using MyProjectsDefaultNamespace.Folder1.Folder2
In an aspx or ascx file, you would use:
<%# Import Namespace="MyProjectsDefaultNamespace.Folder1.Folder2" %>
Never really thought of doing this, but i guess i would do this as follows.
A folder represents a namespace. So where it says inherits="Project.PageName" in your PageName.aspx file, it should state inherits="Project.Folder.Folder.PageName". You also have to change the namespace in your PageName.aspx.designer.cs and PageName.aspx.cs files.
EDIT:
For ASP.Net website simply adjust your CodeFile attribute:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Folder/Folder/Default.aspx.cs" Inherits="_Default" %>
Its not clear if you mean the codebehind .aspx.cs file or a standalone .cs class file.
You can't have .cs files loose in your main folder. It has to be in the app_code folder (or in a class library if you were doing a WAP).
Your .aspx.cs files are paired up with your .aspx file. I wouldn't recommend trying to move these files away if thats what you are trying to do.
The top level namespace that contains _Default or any code that doesnt appear to have a namespace is ASP. This is generally hidden within Visual Studio. So its true name is ASP._Default
And the answer is, obtained from looking into the other answers thanks:
Organise the files into folders within the App_Code folder, they will automatically be included.
Right click on project in solution explore
Go to Add> Add ASP.NET Folder > App_Code
Then add your files/folder structure there
BUT, be warned of what you put in there. Please refer to:
http://vishaljoshi.blogspot.com/2009/07/appcode-folder-doesnt-work-with-web.html
In the properties of the source file(s), you might want to set the "Build Action" property to "Compile"

App_Code and web.config

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?

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

Resources