Why do I "lose context" when I try to move certain operations to class methods?

G

Guest

Hoping someone can help with a simple but puzzling problem. I have some code
that I want to build as a class method. The code works fine when I embed it
in Page_Load. But when I try to generalize the code into the method of a
class I am trying to build, it gives me strange errors: "CS0103: The name
'Server' does not exist in the current context". (Server being a call to
Server.MapPath.)

I'm working in ASP.NET 2.0 and VS.NET 2005.

Here's the code that works just fine:

public partial class aspmht_test1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
aspNetMHT.MHT m = new aspNetMHT.MHT();

string protectedUrl = "http://www.google.com";
m.LoadUrl(protectedUrl);
...
string filename = "new" + DateTime.Now.ToFileTime().ToString() + ".zip";

//save it the filesystem.
m.SaveToFile(Server.MapPath(filename), true); // This is where I get
errors if I move to a class method
}
}

So far, so good. When I try to build a 'Scraper' class, and embed the code
in a method, I get errors on the line notated above. Here's my class code:

public class Scraper
{
public void GetPageAndSave(string remoteurl, string savefilename)

{

aspNetMHT.MHT m = new aspNetMHT.MHT();

m.LoadUrl(remoteurl);
m.Parse();
string filename = savefilename +
DateTime.Now.ToFileTime().ToString() + ".zip";
m.SaveToFile(Server.MapPath(filename), true);
}

The code fails on the last line with the error: CS0103: The name 'Server'
does not exist in the current context.

The code above is in Scraper.cs. Here's how I'm calling this in my page

public partial class aspmht_test2 : System.Web.UI.Page
{

public void Page_Load(object sender, EventArgs e)
{
Scraper myScraper = new Scraper();
string myURL = "http://whatever.com";
myScraper.GetPageAndSave(myURL, "whatever");
}
}

Any help out there? I'm outta ideas....Thanks in advance.

-KF
 
P

Peter Rilling

You have to include the namespace for the Server in the file for your helper
classes. The namespace is defined in the file with your page class, but not
automatically if you create more class files.
 
G

Guest

Thanks. In C#, you're saying I need to include an import directive at the
top of the .cs file? What namespace to I need to be importing (which
namespace manages Server.whatever operations?)

Thanks again.

-KF
 
G

Guest

Ah, the missing piece. Thank you, Juan. I will not need to ask a question
like this in the future!

-KF
 
G

Guest

You may also consider in your method signature passing along the context:

public void YourMethodCall(System.Web.HttpContext context,string path)
{
context.Server.MapPath(path);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top