在虚拟目录中部署mvc3 app后,在路径中找不到图像

时间:2011-10-11 20:49:27

标签: asp.net asp.net-mvc-3

问:以下图片无法解析为.js脚本引用的(js脚本是〜/ scripts,mvc3框中包含所有其他.js脚本)

src: '/content/themes/base/images/down.gif'

这在本地工作,但在作为虚拟目录远程部署到iis6.0服务器时不会。

我试过了:

src: '~/content/themes/base/images/down.gif'

但这不会在本地或远程解决。

如果我对虚拟目录路径进行硬编码(virtualdirectoryname是实际的虚拟目录),以下内容将远程工作

src: 'virtualdirectoryname/content/themes/base/images/down.gif'

2 个答案:

答案 0 :(得分:0)

您无法在javascript中使用~。 尝试:

../Content/themes/base/images/down.gif

答案 1 :(得分:0)

您可以通过几种不同的方式处理此问题。看起来您最好的选择是创建一个Javascript函数,为您提供基本URL,然后使用它来构建您的路径。

以下是http://www.gotknowhow.com/articles/how-to-get-the-base-url-with-javascript的一个示例:

function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));


    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);

        return baseLocalUrl + "/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }

}

然后你的代码看起来像这样:

src: getBaseURL() + '/content/themes/base/images/down.gif'