473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Using Server.MapPath in a Webservice?

Hi there,
I am writing a webservice and am wanting to use Server.MapPath to get
the path to some files on the server that is hosting the webservice. I
am using VB.NET atm, but am learning C#, so either example will be
fine. I have searched all over the newsgroups and forums to get an
example and have found many, but am unable to get any working. I have
got the webservice working, without the call to MapPath. But when I
try a simple example as follows with a call to MapPath it fails with
the error: "Object reference not set to an instance of an object.". I
understand what he error message is saying but don't know how to link
it to the instance, as none of the examples I have seen seem to do
this.

Any help or direction would be much appreciated, thank you for your
time,

Scott.

Here is the code I am using:

Imports System
Imports System.IO
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.WebService
Imports System.Xml.Serialization

Public Class UploadFile

<WebMethod> Public Function Upload(ByVal FlName As String) As String
Try

Dim strFilePath As String
strFilePath = HttpContext.Current.Server.MapPath("/" & FlName)
Return strFilePath

Catch ex As Exception
Return ex.Message
End Try
End Function

End Class
Nov 21 '05 #1
2 23858
Scott wrote:
I am writing a webservice and am wanting to use Server.MapPath to get
the path to some files on the server that is hosting the webservice. I
am using VB.NET atm, but am learning C#, so either example will be
fine. I have searched all over the newsgroups and forums to get an
example and have found many, but am unable to get any working. I have
got the webservice working, without the call to MapPath. But when I
try a simple example as follows with a call to MapPath it fails with
the error: "Object reference not set to an instance of an object.". I
understand what he error message is saying but don't know how to link
it to the instance, as none of the examples I have seen seem to do
this.


I don't see any reason for it to not work, this works for me:

<codeSnippet language="C#">
[WebMethod]
public string MapPathTest()
{
return base.Server.MapPath("~");
}
</codeSnippet>

HTH,
Drew
Nov 21 '05 #2
I have worked it out. The error was actually being generated by the
calling page and it was just coincidence that I change the webservice
code the same time as I changed the calling page code.

Thanks anyway,

Scott.

Drew Marsh <dr****@hotmail.no.spamming.com> wrote in message news:<uh**************@tk2msftngp13.phx.gbl>...
Scott wrote:
I am writing a webservice and am wanting to use Server.MapPath to get
the path to some files on the server that is hosting the webservice. I
am using VB.NET atm, but am learning C#, so either example will be
fine. I have searched all over the newsgroups and forums to get an
example and have found many, but am unable to get any working. I have
got the webservice working, without the call to MapPath. But when I
try a simple example as follows with a call to MapPath it fails with
the error: "Object reference not set to an instance of an object.". I
understand what he error message is saying but don't know how to link
it to the instance, as none of the examples I have seen seem to do
this.


I don't see any reason for it to not work, this works for me:

<codeSnippet language="C#">
[WebMethod]
public string MapPathTest()
{
return base.Server.MapPath("~");
}
</codeSnippet>

HTH,
Drew

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: John Rebbeck | last post by:
I've got the directory f:\Company\Product set as web shared so it's got a virtual directory in the default web site on my test server's IIS. If I try to use Server.MapPath in that site it returns a...
0
by: canasdaq | last post by:
Can anyone please help me. I was looking at the article "http://www.eggheadcafe.com/articles/20030603.asp". I am new to .net and know nothing in c#. I want to write a menu in asp.net. Can anyone...
1
by: Stephen | last post by:
I have the following for a datagrid that inserts a row into an xml file. It works great, but inserts the data to a new top-level node. I don't know how to specifiy an existing node for the...
5
by: David Lozzi | last post by:
Howdy, I wrote a web service in .Net for my customer. My customer has another vendor who now has to consume it but they are not using Visual Studio. Most of their pages are jsp, and they said...
14
by: Lorenzo | last post by:
Hello, I have a web application with a virtual directory in it. With 'virtual directory' i mean a folder whose physical path is different from the physical path of the application, but in which...
4
by: vunet.us | last post by:
How to use server.mappath() parent folder correctly: Server.MapPath("../test.asp") Thank you for the hint.
3
by: rn5a | last post by:
Server.MapPath returns the physical file path that corresponds to the specified virtual path whereas Request.MapPath maps the specified virtual path to a physical path. Assuming that a file named...
13
by: =?Utf-8?B?Um9nZXIgTWFydGlu?= | last post by:
This is a follow-up to my post "Silverlight video doesn't work when file is streamed from handler in ASP.net" at...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.