How to use additional C# source file on page in Web Site project?

Mike32 picture Mike32 · Jun 10, 2014 · Viewed 8.5k times · Source

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.

Answer

Mike32 picture Mike32 · Jun 19, 2014

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!