【问题标题】:Image not found in path after mvc3 app deployed within a virtual directory在虚拟目录中部署 mvc3 应用程序后,在路径中找不到图像
【发布时间】:2011-10-11 20:49:26
【问题描述】:

问:以下图像无法解析为从 .js 脚本引用(其中的 js 脚本是 ~/scripts,所有其他 .js 脚本都在 mvc3 框中)

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'

【问题讨论】:

  • 你的js文件在哪个文件夹?您应该只使用相对路径引用它,以便它适用于所有场景。
  • 脚本与所有其他 scxripts 一起使用:mvc3app\Scripts\ea.droplinemenu.js
  • 本地和远程 Web 根目录的实际完整 URL 是什么?区别在哪里?

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


【解决方案1】:

您不能在 javascript 中使用 ~。 试试:

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

【讨论】:

  • 直接浏览图片有效吗?让我们消除权限问题。
  • 是的,图像在浏览器中根据路径解析,/mvc3app/content/themes/base/images/down.gif。
  • 您提到了一个虚拟目录。是否在 IIS 中设置为应用程序?
  • 是的,我已将其设置为应用程序
  • 对不起,我没有想法。希望其他人可以提供帮助。
【解决方案2】:

您可以通过几种不同的方式来处理这个问题。看起来您最好的选择是创建一个 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'

【讨论】:

  • 这给了我我的基本网址,hostip 而不是hostip/virtualdir。但我需要的是我的工作目录。 virtualdir 映射到包含内容的工作目录。
  • 只需修改它以拉回第二个/然后。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-10
  • 2010-10-11
  • 2020-10-09
  • 1970-01-01
  • 1970-01-01
  • 2016-07-15
相关资源
最近更新 更多