MapPath throws exception

Discussion in 'ASP .Net Web Services' started by cssnow@hotmail.com, Apr 20, 2005.

  1. Guest

    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?
    , Apr 20, 2005
    #1
    1. Advertising

  2. 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]


    <> wrote in message
    news:...
    >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?
    >
    Ken Cox [Microsoft MVP], Apr 21, 2005
    #2
    1. Advertising

  3. Guest

    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
    , Apr 21, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Himmat Dhange
    Replies:
    0
    Views:
    418
    Himmat Dhange
    Aug 26, 2003
  2. Chris Miller
    Replies:
    4
    Views:
    13,283
    Chris Smith
    Nov 22, 2003
  3. =?Utf-8?B?dHBhcmtzNjk=?=

    server.mappath vs. request.mappath

    =?Utf-8?B?dHBhcmtzNjk=?=, Mar 10, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    1,133
    Juan T. Llibre
    Mar 11, 2007
  4. Replies:
    4
    Views:
    953
    Juan T. Llibre
    Oct 7, 2007
  5. Replies:
    2
    Views:
    327
Loading...

Share This Page