views:

18

answers:

1

Hey all,

Its time for another very simple question that I can't find an elegant solution for. Basically, I have an app that is using a jQuery Ajax call. In this call, you have to specify a URL path for the service that you are calling. In this instance, I am needing to call this JavaScript function from multiple files in my application and those files are on differing levels of the folder structure.

Here's the question, how would you elegantly handle this scenario so that you can call the JS function from any location in your app. Here are my constraints:

1) I am running on Asp.Net 4.0. 2) My current environment has a local, Dev, Test, and Prod Environment (hard-coding the URL path will not work).

Code Snippets:

    function MakeTheCall() {

  $.ajax({
   type: "POST",
   url: "Services/FileName.asmx/Handler", //Path in Question
   data: "",
   contentType: "application/json; charset=utf-8",
   dataType: "json",
   success: function(result) {
   },
   error: function(xmlHttpRequest, status, err) {
   }

  });
}

I would like to be able to call this function (which is in an external JavaScript file) from files in different directory levels, such as:

1) http://SomeDomain.com/SomeDir/CallingFile.aspx 2) http://SomeDomain.com/CallingFile.aspx

Any suggestions? I can think of a couple different scenarios for pulling this off, but most revolve around creating a JavaScript variable and setting its value in C#. Hopefully there's a better way?

Thanks!

+1  A: 

Have you tried url: "/Services/FileName.asmx/Handler"?

Edit: The applicability of the above depends on whether you have the "Services" directory always on the same level relative to the domain name or not. If you deploy the whole application under a different path level then you'd need to use

// in code-behind file
string rootURL = HttpContext.Current.Request.Url.GetLeftPart(System.UriPartial.Authority) + 
    "/" + HttpContext.Current.Request.ApplicationPath + "/";

// in .aspx
url: <%Response.Write(rootURL)%> + "Services/FileName.asmx/Handler"
Saul
Yes, I when we/ the "/" as part of the initial path. The one downside is that one of my environment is actually set as a virtual directory rather than its own website, which adds a new level of pain. :/ Think option 2 is going to have to be my route.
Nathan