views:

1029

answers:

4

I am loading up some content via jQuery.load('/Business/Tags'), which works well when using my local webserver. But when using iis this does not resolve correctly. I have seen a couple different ways to get around this such as ResolveUrl. What is the best way to handle resolving urls in your javascript?

+9  A: 

I ran into this exact problem with my JavaScript. Since my JavaScript was also in a separate file which was included in my Master Page, I couldn't use ResolveUrl in my case.

Here's how I solved it in my case. I included the following JavaScript in my head tag in my Master Page at the top:

<script type="text/javascript">
    var baseUrl = "<%= Request.Url.GetLeftPart(UriPartial.Authority) %>";
</script>

I'm setting a global variable that's accessible to all my JavaScript files called baseUrl.

Your code would then become:

jQuery.load(baseUrl + '/Business/Tags');
Praveen Angyan
I tried this method but I am not getting quite what I need but I think I can work with it. I'm getting a string like this "http://mtsengv003/" but I need one like this "http://mtsengv003/MVC/" Thanks
I think it's better to use:var baseUrl = "<%= Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath.TrimEnd('/') %>";
eu-ge-ne
@eu-ge-ne: Thanks for pointing that out. My memory isn't what it used to be. Your code is perfect.
Praveen Angyan
+4  A: 

As for me I'm using Url.Action/Url.RouteUrl helpers with JavaScript/jQuery where it is possible:

$.load('<%= Url.Action("Tags", "Business") %>')

or

$.load('<%= Url.RouteUrl("BusinessTagsRoute") %>')

In other cases I'm using ResolveUrl helper (from MVCContrib or your could write your own):

$.load('<%= Url.ResolveUrl("~/Business/Tags") %>')

UPDATED:

It is also possible to create special controller for your JavaScript/jQuery with actions, returning PartialViews with JavaScript:

public ActionResult YourJavaScript()
{
    Response.ContentType = "application/x-javascript";
    return PartialView("YourJavaScript");
}

Then in your YourJavaScript.ascx you can use JavaScript/jQuery code with WebForms server tags. I still recommend not to use hardly coded Urls in jQuery/AJAX

eu-ge-ne
If you place all your JavaScript in a separate file and then reference this file using Url.Content in your Master page, Url.ResolveUrl will not work within your JavaScript. Someone please correct me if I'm wrong about this.
Praveen Angyan
+1  A: 

I still recommend to using base href tag in your Master page. therefore any images or javascript calls will resolve from this url. This will allow you to put your javascript functions/calls in an external js file.

<html>
    <head>
        <base href="http://www.mysite.com/virtual_dir1/"&gt;
        <script src="location_of_js_file">

Remember the ending '/' in the base href.

Also, remove the leading '/' and whenever you do jQuery.load('Business/Tags'), it will actually come from http://www.mysite.com/virtual_dir1/Business/Tags.

David Liddle
I really really like this approach. So clean and requires so little effort. Thanks
lomaxx
A: 

A couple of ways we do this in our apps:

 var applicationPath = '<%= Url.Content("~/") %>';

OR

 var applicationPath = '<%= Request.Url.Scheme %>://<%= Request.Url.Host %><%= Request.ApplicationPath %>/';

We then use applicationPath as the base url for all our ajax calls.

Sam Wessel