4

Edit: { I think I've added a lot (too much) of information (reading) here. The important bits of what I want is:

  • I'm using a Web Site (no .csproj file)
  • I need multiple source files of code for my ASPX to run
  • From what I understand I either need to use a pre-compiled DLL or tell .NET to compile the additional files on-the-fly when an ASPX file is requested
  • I'm not sure how to get either situation to work
  • If providing help on a DLL, please tell me what statement to use and put in what file (ASPX directive, web.config, etc -- can't use .csproj)
  • If providing help on the other option, please provide the statement to use to include MULTIPLE source files, and where to put the statement (ASPX directive, web.config, etc, -- can't use .csproj) -- I'm already using @Page CodeBehind/CodeFile, I need more than one source file

}

I'm trying to include additional C# source files to my ASPX file, it is not working.

I have Default.aspx (with @ Page CodeFile="Default.aspx.cs") and would like to reference other files (e.g. LinkList.cs, SQLiteDB.cs, SQLServerDB.cs, JSON.cs).

I'm thinking it should be something like this (in the Default.aspx file):

// <%@ Reference Page="secondfile.cs" %>

I am new to ASP/.NET but have some experience with C# and am very experienced with PHP, JS, client-server model, etc. I have tried various ways to include them but to no avail; attempts listed below.

My setup uses a Web Site (compiled on demand; App vs. Site), using a text editor (NOT VS!) to edit the remote files. I started with VS but determined after hours of debugging that my server requires various different settings than my VS project and I do not want to maintain multiple versions of the same code... As such, and as I am on a deadline, even if I could get VS to work with my setup (comments welcome), my primary concern is to just get the code working with the server.

Things that do work:

  • all the code from all the C# files merged into one, my CodeFile, does compile; the issue is being able to have each class in a separate file for maintainability.
  • as determined from the above link, I need to use @Page CodeFile (NOT CodeBehind); I have determined that this does cause the primary C# file to load

Things that don't work:

  • HTML script tag (link); this method does cause the other files to attempt compilation, but get an error on the 'using's in the files, but even when I move the 'using's to the primary source file, the classes in the other files are not recognized as data types in the primary C# file.
  • <%@ Reference Page="secondfile.cs" %> (error: "The file 'src' is not a valid here because it doesn't expose a type."; not sure where it's getting 'src' from, I'm not using it and 'src' isn't a valid attribute to Reference...)
  • the 'App vs. Site' link specifies that all the files in the directory are included in the compilation, but the other files are not being compiled... all my files are in /www/dev/*, and all my C# files use the same namespace
  • <%@ Assembly Name="assemblyname" %> or <%@ Assembly Src="pathname" %>

actually using the following does seem to cause the files to load, but I still get an error: "CS0246: The type or namespace name 'SQLServerDB' could not be found (are you missing a using directive or an assembly reference?)"

    <%@ Assembly Src="SQLServerDB.cs" %>
    <%@ Assembly Src="DataSource.cs" %>
  • including "using myNS.SQLServerDB" produces the same error

I'm not sure what else to try... please help. I've tried various combinations with other asp directives too.

Update/Clarification: What I'm asking is since I'm using a Web Site setup (NOT Web Application), how can I tell the ASP Server to compile other source code with the CodeFile? Answers so far do not address both aspects of this situation. The 'using' command doesn't tell the compiler where the code files are, just desired classes.

Mike32
  • 111
  • 1
  • 9
  • Each Page can only have a *single* source file directly associated with it - this *is* the "Code Behind". The page may reference additional assemblies but it *cannot* "include" additional source files directly. – user2864740 Jun 10 '14 at 00:37
  • I'm aware of that. But I need to reference other source files, why is this not possible???? How do I reference the other code: how do I make the assembly? Just compile the other C# files to a DLL (how?) and put it on the server and at an @Reference to it? – Mike32 Jun 10 '14 at 00:41
  • Because it's not possible. The page directives are [discussed here](http://msdn.microsoft.com/en-us/library/vstudio/xz702w3e(v=vs.100).aspx). Yes, building and distributing a DLL is one option: an applicable ASP.NET Project should already support this. (Reference is used for other Pages/Controls, not for arbitrary source files.) – user2864740 Jun 10 '14 at 00:41
  • No it's not CodeBehind. I included the wrong link, see [here](http://support.microsoft.com/kb/312311). " The .NET Framework does not actually use this attribute. Instead, Visual Studio .NET uses this attribute to maintain a reference to the associated code-behind file for the .aspx page." This link says to use Src, but [another source](http://alturl.com/3gpvu) mentions CodeFile, and despite what you think is correct, CodeBehind is NOT working for my setup but CodeFile IS. – Mike32 Jun 10 '14 at 00:50
  • I am not sure what you are trying to achieve. If you want to use some other class in some library/dll, then you can just use the using statement. search for using statement in google – Ehsan Jun 10 '14 at 00:50
  • That is the page I've been using but I couldn't add more than two links to my post... Okay, so does MS expect me to include ALL my code in one C# file? That doesn't make any sense. But an ASP.NET Project is just screwing up the configuration, for starters, I have to alter all my file paths when I move my code to the server, because the Project is a project and my server doesn't support it. So how should I include the DLL (assuming I figure out how to create it) in my ASPX? Use the @Reference directive? – Mike32 Jun 10 '14 at 00:56
  • Yeah I meant to mention that more in my original post, the using statement doesn't work because the object/class is in a different file. If I could reference the other file then I'd be good. I actually am not needing a using because all the code is in the same namespace, I just need to get all the source files to load. – Mike32 Jun 10 '14 at 00:58
  • I'm trying to achieve, for example: Default.aspx (serves html), Default.aspx.cs (creates html), MyDBLibrary.cs (gives access to my database), MyDataStructures.cs (gives me access to other objects). I can get Default.aspx and Default.aspx.cs to work, BUT I also need to include MyDBLibrary.cs and MyDataStructures.cs for my app to work. I have determined I could put the code from all these files into one file (Default.aspx.cs) and it will run, but I don't want one 5,000 line file, I want 5 1,000 line files. – Mike32 Jun 10 '14 at 01:03
  • I've changed title - please make sure it matches what you trying to achieve. – Alexei Levenkov Jun 10 '14 at 02:02
  • On a side note, VisualStudio is best off working with a local copy of a project/website. This is then published to the server. Working with a local copy will give you better debugging opportunities. Not only that, you are no longer playing with fire by editing live production code! – Jon P Jun 10 '14 at 02:18
  • Yes I would agree. I just don't know what changes to make in my project in VS that will actually get interpreted on the server since I can't use a .csproj file. I seem to be limited to settings in the ASPX file and the Web.config file. But I would think that I should be able to use at least one of those locations to tell .NET to include additional source files with the on-demand compilation... – Mike32 Jun 13 '14 at 06:57

4 Answers4

4

Here is two solutions that presently work for me. Unfortunately I tried these previously and they didn't work. I don't have access to the web server setup so I don't know why they didn't work previously.

  1. As @Alexei Levenkov suggested, use the App_Code directory. This had not previously worked for me, and since I'm new to ASP.NET and every other language I have ever used requires somehow explicitly specifying the needed source files, this solution is foreign and I am completely not surprised that this did not make sense at first. Also, (almost) all the documentation I found referred to the App_Code directory in the context of a Web Application, and so I disregarded it as it seemed to be just a standard name and I could use whatever I wanted, not a magic name respective to .NET.

  2. The other solution is to use the following directly under the @Page directive in the .ASPX file. Note: using a path in "App_Code" only works in whatever situation causes .NET to not register "App_Code" as a magic directory; I do not know what these situations are, I just know that they are possible and I encountered them.

<%@ Assembly Src="App_Code/Utilities/SQLServerDB.cs" %>

<%@ Assembly Src="App_Code/Utilities/Functions.cs" %>

<%@ Assembly Src="App_Code/Utilities/Database.cs" %>

<%@ Assembly Src="App_Code/Utilities/LinkedList.cs" %>

<%@ Assembly Src="App_Code/DataObjects.cs" %>

Also note: these solutions I found are namespace irrelevant. I mean, these solutions cause other source files to be linked together when compiled, and has absolutely nothing to do with the structure of the code. Any files could be in any accessible directories and could all be in the same namespace or different ones. Comments by others suggesting namespaces did not work because this was not the issue I was having. Each source file DOES need a "using" statement to reference other namespaces though; but this has nothing to do with where the source code is located.

Thanks to all those who made suggestions!

Mike32
  • 111
  • 1
  • 9
1

You can add other class files (e.g. LinkList.cs, SQLiteDB.cs, SQLServerDB.cs, JSON.cs) and then instantiate these classes from Default.aspx.cs.

wind39
  • 441
  • 4
  • 14
  • Yes, that is exactly what I'm trying to do. But I don't know how to do that. I mean I can't reference LinkedList object unless Default.aspx.cs knows where the LinkedList object/class is (LinkedList.cs). So how do I tell ASP/C# that I need the code in LinkedList.cs? – Mike32 Jun 10 '14 at 01:29
  • Can you use another IDE, such as MonoDevelop or SharpDevelop? If you could get a .csproj, then compile the whole thing would be easy with xbuild. – wind39 Jun 10 '14 at 01:37
  • You see, the project needs to know there is such class files. So, you have to instantiate the needed class on the Default.aspx.cs file, knowing which namespace this class is. – wind39 Jun 10 '14 at 01:39
  • How "you can add other files" is different from steps mentioned in question? – Alexei Levenkov Jun 10 '14 at 02:04
  • I can use any editor I want, but I have no control over the server setup. And since the setup is a Web Site, I can't use a .csproj file. – Mike32 Jun 13 '14 at 00:14
  • Instantiating the needed class does not tell the compiler where the source code for the class is. I need a way to tell the compiler to include other source files to compile as well. As stated in the question my setup is a Web Site, as such I do not have a project. – Mike32 Jun 13 '14 at 00:16
  • @AlexeiLevenkov steps listed in question do not work, as specified in the question. – Mike32 Jun 13 '14 at 00:18
  • @Mike32 yes... but somehow wind39 decide to post essentially the same steps as an answer - I guess you understood meaning of this answer unlike me. Also I'm clearly completely confused about this question - so ignore my comment (as wind39 did). – Alexei Levenkov Jun 13 '14 at 01:01
  • Sorry, guys, one can compile C# applications without the need of an IDE, but I really couldn't figure out how to compile an ASP.NET without an IDE. Please ignore my answer. – wind39 Jun 13 '14 at 12:41
1

NOTE: I've just noticed the question rules out using Visual Studio and this is a website as opposed to a web app. Therefore this answer will be of limited use to the question as posed. It may be usefull to others who stumble accrross this question.

Include each of the additional .cs files as part of the project. This will compile into the sites .dll on build. In each of the these additional .cs files there will be a class and perhaps a namespace as well.

Step 1: To include the .cs files, right click on the Project Name in Visual Studios' Solution Explorer. Select "Add > Existing Item" and navigate to the .cs Files. You may want to use the dropp down on the "Add" button and sleect "Add as Link"

Step 2: You will more than likely need to use the using statements to access these classes in your Default.aspx.cs file. E.g:

using System.Web.UI.WebControls; /*This should already be there**/
using [NameSpace].LinkList;
using SQLLiteDB;

Step 3: You should now be able to instantiate these classes in your Default.aspx.cs file. E.g:

LinkList myList = new LinkList();

Note that using server controls works very differently. That is if LinkList is a control that genrates some HTML the way to use this is Very different.

Jon P
  • 19,442
  • 8
  • 49
  • 72
  • +1: Note that while this is possible approach (hence answer), it does not directly address "how to use multiple CS files for same page (similar to assemblies) in Web Site project". – Alexei Levenkov Jun 10 '14 at 01:59
  • Thanks @AlexeiLevenkov, not only that, it seems Visual Studio isn't an option as well. This won't help the OP I'm afriad but may help others. So I won't delete it. – Jon P Jun 10 '14 at 02:03
1

I believe you are looking for App_code folder.

In a Web site project, you can store source code in the App_Code folder, and it will be automatically compiled at run time. The resulting assembly is accessible to any other code in the Web application. The App_Code folder therefore works much like the Bin folder, except that you can store source code in it instead of compiled code.

Put you shared classes into App_Code folder and they should be compiled/visible for all pages.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • As stated in the question, the server is setup as a Web Site, not a Web Application. So I don't have an App_Code folder. – Mike32 Jun 13 '14 at 00:08
  • @Mike32 I thought article explicitly talks about Web Site projects, but you say it is Web Site in your case and it somehow different... Not sure what you mean than... I'm somewhat confused, sorry about that. – Alexei Levenkov Jun 13 '14 at 00:58