views:

217

answers:

3

I have a solution with several projects, one a web site the other a separate web server. (They will reside on different hosts.) I need to connect to the web service from some javascript in the web site. However, although this seems like an obvious, easy thing, I can't seem to get it right. My web service works fine, I can connect to it from a Windows forms app in a separate project in the same solution. What I have in my aspx file is this:

<asp:ScriptManager ID="ScriptManager1" runat="server">
    <Services>
        <asp:ServiceReference Path="~/../WebService/WebService.asmx" />
    </Services>
</asp:ScriptManager>
<script type="text/javascript">
    function calback(result)
    {
        alert("hello");
    }
    WebService.FunctionToCall("hello", callback);
</script>

However, the service reference says it can't access .. from ~

Can someone point me in the right direction?

A: 

Try using the full path instead of ~/../

mcandre
If I do that it is tied to the specific path on my computer, and so other people can't work with it.
A: 

~ (root) operator denote a path of web application. If webservice folder exists in your root of an application then attribute value of path can be Path="~/Webservice/WebService.asmx"

adatapost
In my case ~ refers to the root of the web application, which is a project within a solution. The web service is in a separate project, so the asmx file is not below the ~ for the web application. So I have: Solution Directory | +--- Solution.sln | +--- Web Application Project | | | +--- file-using-web-service.aspx | | | +--- Web Application.csproj | +--- Web Service Project | +--web-service.asmx | +--WebService.csproj
The formatting of that previous comment got screwed up.Basically, under the solution directory I have the solution file and two directories: web-app and web-service.Under web-app I have an aspx file that needs to call the web service.Under web-service I have an asmx file that provides the web service.So ~ in the aspx file refers to the web-app directory under solution. The web service is in the directory web-service with is .. from the aspx file.
A: 

The fact that these two projects are in the same solution is irrelevant. You're hosting them on two different servers. Only the URL will matter.

Since the URL will differ between your development machine and the production (and no doubt, the test) machines, you'll have to handle that like any other URL that changes based on the environment - you'll need to use the config file.


I haven't completely tested this, but it looks like it's working:

<asp:ScriptManager ID="ScriptManager1" runat="server" ScriptMode="Debug" >
    <Services>
        <asp:ServiceReference Path="<%$ AppSettings:webServiceUrl %>"  />
    </Services>
</asp:ScriptManager>

in web.config:

  <appSettings>
    <add key="webServiceUrl" value="http://localhost:38759/Enumerations.asmx"/&gt;
  </appSettings>

This actually did generate a script reference to the above web service.

John Saunders