代码之家  ›  专栏  ›  技术社区  ›  Mike Roosa

为什么我的图像/脚本不会显示在ASP.NET MVC部署的站点上?

  •  1
  • Mike Roosa  · 技术社区  · 16 年前

    我已将应用程序部署到IIS6服务器。目前,我正在使用通配符映射。我的应用程序在我的开发机器上运行得很好,但是当我尝试在服务器上访问它时,有些页面工作,有些则不工作。

    是脚本和图像给了我最大的问题。

    我有一个网址 http://localhost/sdev/home/index 除了图像和脚本没有加载外,页面显示得很好。当我查看源代码并查看URL时,我看到:

    ../../Content/Images/logo.png
    

    如果我尝试导航到该URL,它将尝试转到

    http://localhost/content/images/logo.png
    

    而不是

    http://localhost/sdev/content/images/logo.png
    

    奇怪的是有些页面工作正常,例如:

    http://localhost/sdev/ServiceCall/DivisionStep/ALB?type=fsr
    

    我能做些什么来解决这个问题吗?是的,我读过菲尔的指示,认为我正确地遵循了它们,但也许我遗漏了一些东西。

    3 回复  |  直到 16 年前
        1
  •  1
  •   liammclennan    16 年前

    使用

    <%= Url.Content("~/Content/Images/logo.png") %>
    

    要生成URL,您应该可以。

        2
  •  1
  •   Matt Kocaj    16 年前

    我刚写了一些帮助图片你可以使用。

    (1)只需使用 using System.Web.Mvc; 并将其添加到MVC项目中名为“helpers”的文件夹中。

    (2)采用以下方法复制:

        public static string Image(this HtmlHelper helper,
            string classText, string sourcePath, string altText, string width, string height)
        {
            return Image(helper, classText, sourcePath, altText, width, height, null);
        }
    
        public static string Image(this HtmlHelper helper,
            string classText, string sourcePath, string altText, string width, string height, object htmlAttributes)
        {
            StringBuilder sb = new StringBuilder();
            if (htmlAttributes != null)
                foreach (PropertyInfo p in htmlAttributes.GetType().GetProperties())
                    sb.AppendFormat(@" {0}=""{1}""", p.Name, p.GetValue(htmlAttributes, null).ToString());
    
            if (htmlAttributes == null)
                return String.Format(@"<img{0} src=""{1}"" alt=""{2}"" width=""{3}"" height=""{4}"" />",
                    String.IsNullOrEmpty(classText) ? String.Empty : String.Format(@" class=""{0}""", classText),
                    (new UrlHelper(helper.ViewContext.RequestContext)).Content(sourcePath),
                    altText, width, height);
            else
                return String.Format(@"<img{0} src=""{1}"" alt=""{2}"" width=""{3}"" height=""{4}""{5} />",
                    String.IsNullOrEmpty(classText) ? String.Empty : String.Format(@" class=""{0}""", classText),
                    (new UrlHelper(helper.ViewContext.RequestContext)).Content(sourcePath),
                    altText, width, height, sb.ToString());
        }
    

    (3)..和这样使用: <% =Html.Image("small_pic_border","~/Content/Images/Home/office2_137x139.jpg","principal headshot","137","139") %>

    此方法使用LiamClenan提到的url.content方法。它还应该迫使你进入一些好的习惯:比如使用替代文本等。

    对于脚本,请使用: <script type="text/javascript" src="<% =Url.Content("~/Scripts/mootools.js") %>"></script>

        3
  •  0
  •   Eric Petroelje    16 年前

    而不是这样做:

    ../../Content/Images/logo.png
    

    这样做:

    /sdev/Content/Images/logo.png
    

    更好的是,在代码后面生成该URL的第一部分(/sdev),因为听起来该部分可能会更改(我猜这里的“sdev”是网站的某种开发版本,对于生产版,在URL前面不会有“sdev”)。

    在第一个示例中,它不起作用的原因是浏览器将其视为在“sdev/home”目录中查找名为“index”的文件。所以向上移动两个目录会将您带到根级别。

    不过,对于“/sdev/servicecall/divisionstep/alb”来说,它工作得很好,因为您现在正在查看“/sdev/servicecall/divisionstep”目录中的“alb”,向上两个级别将进入“/sdev”