MapPath throws exception

C

cssnow

I created the following method in a webservice on my localmachine web
host to test MapPath because it throws an exception in another method:

public class ProgramUpdateService : System.Web.Services.WebService
{
....
[WebMethod]
public string TestMethod()
{
string strtest = Server.MapPath("..\\");
return strtest;
}

I call the testmethod from a test client I have created. The client is
also running from my machine. The exception is an InvalidOperation,
and the message is: "HttpContext is not available. The class can only
be used in the context of an ASP.NET request."

As a side note, MapPath works fine when I call the method from a
browser by entering:
http://localhost/TestWVWProgramUpdate/TestWVWProgramUpdate.asmx
as the address, and using the form that ASP.NET creates.

I suppose that what the exception is telling me is that the code is not
running inside ASP.NET when called from the test client I created. How
is that possible?
 
K

Ken Cox [Microsoft MVP]

Something doesn't look right in the syntax of your example. Is it that
you're passing a relative path ("..") rather than the virtual path on the
Web server that is expected by mappath?

It may be working when the path is NULL, because MapPath then returns the
full physical path.

String FilePath;
FilePath = Server.MapPath("/MyWebSite");

Let us know?

Ken
Microsoft MVP [ASP.NET]
 
C

cssnow

Ken,

I used your example, and received the same exception. I've used the
relative paths in aspx codebehind pages before, and I've had no
problems. The relative path starts in the working directory(in this
case bin). Incidentally, your example works fine when I call my method
from the page that ASP.NET creates, but my code also works in that
situation. I also get the exception when I give MapPath an empty
string as a param.

Rob
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top