简体   繁体   中英

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

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

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!

您可以添加其他类文件(例如LinkList.cs,SQLiteDB.cs,SQLServerDB.cs,JSON.cs),然后从Default.aspx.cs实例化这些类。

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

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM