Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP.NET C# multiple classes 1

Status
Not open for further replies.

BoostMR2

IS-IT--Management
Jul 20, 2007
33
US
I have the following program flow for my web app i am developing. All the files are in the same directory, Webtools\<filename>

WebCheck.aspx (inherits WebCheck.aspx.cs)
WebCheck.aspx.cs
NSLookup.cs (custom class for resolving hostnames/IPs)

WebCheck.aspx.cs looks like this:

using Statements......

namespace WebTools
{

public class WebCheck : Page
{

public void Start()
{
........some code
NSLookup.isHostAddress(inputSite)
}
}
}


NSLookup.cs looks like this:

namespace WebTools
{

public class NSLookup
{

// Determine if it's a Host Address
public bool isHostAddress(String address)
{
....some code
}
}
}

I recieve the following compilation error:
CS0103: The name 'NSLookup' does not exist in the current context

If I try to add, as a test:
NSLookup look = new NSLookup();
I recieve CS0246: The type or namespace name 'NSLookup' could not be found (are you missing a using directive or an assembly reference?)

So it's obviously just not fidning anything called NSLookup

I thought that the 2 classes could see eachother reguardless of being in different files. I think I am missing something because of the ASP.NET environment.

I know that there is something I need to do so that WebCheck.aspx.cs knows where the NSLookup class is. But how do i accomplish this?

Note: this worked fine when the 2 classes were in the same file. I moved the NSLookup class over to another file.

 
You need to instantiate your class file in the page:
NSLookup NS = new NSLookup();
Then in your page:
NS.isHostAddress(inputSite)

 
That much i understood, and in my first post I had mentioned that i tried that, recieving this error message:

CS0246: The type or namespace name 'NSLookup' could not be found (are you missing a using directive or an assembly reference?)

Again, I am going cross-file. I know that if I put the NSlookup class in the same file as the WebCheck class, this would work no problem, as it did before. But now that I have taken the classes and put them in different files, they do not work. Am I missing an import statement, like HTML uses? I honestly don't see how else WeCheck.aspx.cs knows where NSLookup.cs resides. When uysing a compiler, you would specify all files involved. In ASP.NET this code is not precompiled, so how does it know where the other files/classes are?
 
You need to put NSLookup.cs into the APP_CODE folder of your website. All custom classes are required to be put there if you intend to reference them at any point.

Cheers,

G.

Oxigen - Next generation business solutions
-----------------------------
Components/Tools/Forums/Software/Web Services
 
I will try to figure that out. My site does not have any specified folder for custom classes right now. I was using an asp <@ Inherits="WebTools.WebCheck" SRC="<filepath>" >

I tried adding a second inherits line, but it doesn't appear to work. I'll post an update as soon as I can
 
Well, I have location my App_Code folder under the framework folder on my web server. The default .cs files were in there. I moved my NSLookup.cs file to this directory, so it would be precompiled automatically and the class 'NSLookup' would be usable by my web-app. I am still recieving the same error though. The class isn't available to be used. I do believe I need to include a using statement in my WebCheck.aspx.cs file like 'Using NSLookup;' or soemthing to that effect, but not sure what to put. Any ideas, or am I headed in the wrong direction with this?
 
Remove the <%@ Inherits %> directive from the ASPX file, and follow through below. One of these should help solve the problem.

Is this an addition to an existing website? or have you registered this app as a new website in IIS? In either case, the APP_CODE folder should reside in the main folder of the website itself.

For instance, if I created a new website in C:\INETPUB\ the APP_CODE folder should reside in C:\INETPUB\
If you placed app into an existing IIS ASP.NET website, then you should place the NSLookup class file into that sites APP_CODE folder. For example... If a site exists under C:\INETPUB\ and you placed your app into C:\INETPUB\ then you should probably put the .CS file into the C:\INETPUT\ folder.

If the existing website is NOT an ASP.NET site, but your app is, you should declare your app's folder as a new Application inside this existing website, and copy the NSLookup class file into the APP_CODE folder in the application directory, not the root folder. For instance... if you have C:\INETPUT\ as a website and added C:\INETPUB\ as a new Application inside of the main site, then place the custom class under the C:\INETPUB\ folder.


Oxigen - Next generation business solutions
-----------------------------
Components/Tools/Forums/Software/Web Services
 
The website is new, and it resides as e:\WebTools\WebCheck\WebCheck.aspx
I first tried to create e:\WebTools\WebCheck\App_Code
I placed the NSLookup.cs file in this App_Code
this did not appear to work, but I will test again. When this is done, do i need to add a reference to the main .cs codebehind page?

Just incas,e here is my website hierarchy "plan"

WebCheck.aspx
|
WebCheck.aspx.cs
/ | \
Ping.cs NSLookup.cs Traceroute.cs

So in essence my website has a single code-behind "control" file that then uses some of my own custom classes.

WebCheck.aspx.cs cannot find any of the classes from the other CS files. I just want to make sure this issue is understood, I'm not the best at explaining these things, but I hope this clarifies.

There are other sites running from this server, and for that reason there are different application pools (this site has it's very own app pool). I doubt this has any effect, but I am about at a loss as it is. I am doing some further testing, I appreciate the help.

If I can't get this thing running by EoD, I'm just going to compile my classes into a library and reference them. I want to avoid this, but we'll see.
 
Hi there,

You mention that your webpage lives in e:\WebTools\WebCheck\WebCheck.aspx, but in IIS, what is the home folder for the website? is it e:\WebTools?

What I am trying to figure out is, if you were to access this through the web, would I be typing


or




Oxigen - Next generation business solutions
-----------------------------
Components/Tools/Forums/Software/Web Services
 
Yup, no go on that solution. I created the directory structure, as such:
WebCheck
WebCheck.aspx
WebCheck.aspx.cs
App_Code
NSLookup.cs

WebCheck.aspx.cs has the following definition:

using System;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;


public class WebCheck : Page
{
protected System.Web.UI.WebControls.Label Host;
protected System.Web.UI.WebControls.Label IP;
protected System.Web.UI.WebControls.Label Ping;
protected System.Web.UI.WebControls.Label ErrorMsg;
protected System.Web.UI.WebControls.Button Submit;
protected System.Web.UI.WebControls.TextBox URL;
protected System.Web.UI.WebControls.TextBox Proxy;

String strErrorMsg;

//--------------------------------------------------
// Control Method - executes necessary tests
//--------------------------------------------------
public void Start(object o, System.EventArgs e)
{
NSLookup lookup = new NSLookup();
....input stuff
bool isHost = lookup.isHostAddress(input);
....lots more code
}
}


NSLookup.cs has the following definition:

using System;
using System.Net;
using System.Net.Sockets;

public class NSLookup
{
NSLookup()
{
}

// Determine if it's a Host Address
public bool isHostAddress(String address)
{
.....lots more code
}
}


CS0246: The type or namespace name 'NSLookup' could not be found (are you missing a using directive or an assembly reference?)



There must be something super-simple I am missing. This is broken down as simple as I can make it, and I don;t see why, if infact the class "NSLookup" is precompiled by the app_code folder, it isn't being found by my control class. I even restarted IIS on my server to see if it would kick-in. I also verified that this site specifically is running asp.net 2.0. Everything appears fine.
 
As a reply to the previous post, the home folder is webtools. Inside web tools is the webcheck folder and a default little webpage called webtools.htm. here is the complete structure:

e:\WebTools
WebTools.htm
WebCheck
-WebCheck.aspx
-WebCheck.aspx.cs
App_Code
NSLookup.cs

So the Home Folder for the site is webtools, and when you open the site by default, it shows webtools.htm. Then I click on the link on this page to bring me to Webtools/WebCheck/WebCheck.aspx
 
You know what guys, that last post by Gorkem knocked some sense into me. I haven't used the app_code folde rbefore, but I remembered reading that it has to be in the root of the site's "root folder". My App_Code folder was on the root of the currect page, but not hte root of the site, which is why it wasn't working. I am now getting my methods without a problem.

I appreciate all the help.
 
Glad I could help!

Cheers,

G.

Oxigen - Next generation business solutions
-----------------------------
Components/Tools/Forums/Software/Web Services
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top