Question

I have a Javascript function, where I want to call jQuery.Load() to load a file. How would I get ASP.Net to fill in the local server name, so that I can just give a relative path?

The reason for that is I can give the base, say "http://www.mydomain.com/", however I would like to be able to test locally and not have to publish on every single build. I just want to load a local file.

My first thought was just "/MyFolder/MyPage.aspx", but that did not work. I then thought of "~/MyFolder/MyPage.aspx", but that did not work either.

I figure it should be some sort of ASP.Net directive to prepend, just I am not sure what.

I wanted to give some code to show the actual use and what worked.

<head runat="server">
   <script type="text/javascript">
        // <![CDATA[
         function DoPopupSignin()
        {
            var urlLoad = "http://" + window.location.host + '/Candiates/Login.aspx';

            // Triggering bPopup when click event is fired
            $('#popupSigninMaster').bPopup({
                //modalClose: false,
                //opacity: 0.6,
                //positionStyle: 'fixed', //'fixed' or 'absolute'
                content: 'iframe', //'iframe' or 'ajax'
                contentContainer: '.content',
                loadUrl: urlLoad, //Uses jQuery.load()
        });
        }
        // ]]> 
    </script>
</head>

I am trying to get my jQuery Popup working loading the contents from another file. My code uses the free jQuery popup control that I found jQuery.bPopup.js.

Était-ce utile?

La solution

In JavaScript as you are preparing your query, you can use window.location.host.

var path = window.location.host + '/relative/path/file.ext';
$.load(path);

Autres conseils

It's a recurring pain, but I use something like this:

public static Uri ToAbsoluteUri(this string path)
{
  Uri uri;
  if (Uri.TryCreate(path, UriKind.Absolute, out uri)) return uri;

  var serverUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);
  var serverUri = new Uri(serverUrl);
  return new Uri(serverUri, path);
}

Controls / Forms in .NET have a ResolveUrl(string) method that will allow you to resolve relative paths using ~/ pathing. Otherwise you use the VirtualPathUtility static class.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top