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
Peter Rilling - 24 Jul 2005 08:53 GMT
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.
> 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
[quoted text clipped - 61 lines]
>
> -KF
kenfine@u.washington.edu - 24 Jul 2005 09:07 GMT
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
> You have to include the namespace for the Server in the file for your
> helper
[quoted text clipped - 73 lines]
>>
>> -KF
Juan T. Llibre - 24 Jul 2005 12:40 GMT
Keep this link handy...
The .Net Framework 2.0 Class Browser :
http://beta.asp.net/QUICKSTART/util/classbrowser.aspx
MapPath is a method in System.Web.HttpRequest.
http://beta.asp.net/QUICKSTART/util/classbrowser.aspx?assembly=System.Web,%20Ver
sion=2.0.0.0,%20Culture=neutral,%20PublicKeyToken=b03f5f7f11d50a3a&namespace=Sys
tem.Web&class=HttpRequest
Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
> 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
[quoted text clipped - 76 lines]
>>>
>>> -KF
kenfine@u.washington.edu - 24 Jul 2005 17:15 GMT
Ah, the missing piece. Thank you, Juan. I will not need to ask a question
like this in the future!
-KF
> Keep this link handy...
>
[quoted text clipped - 101 lines]
>>>>
>>>> -KF
Clint Hill - 24 Jul 2005 21:02 GMT
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);
}

Signature
Clint Hill MCAD
H3O Software
http://www.h3osoftware.com
> Ah, the missing piece. Thank you, Juan. I will not need to ask a question
> like this in the future!
[quoted text clipped - 106 lines]
> >>>>
> >>>> -KF