【问题标题】:Fonts and images are not found in release mode发布模式下找不到字体和图片
【发布时间】:2015-10-17 09:47:06
【问题描述】:

我有一些内容(图像、字体等)的 Web 应用程序。我试图查看它在发布模式下的工作方式,所有布局崩溃导致字体和图像“未找到”。

什么可能导致此行为以及如何解决?

我发现了什么 - 我的图像在文件夹 Content/images/ 中。在调试模式下,应用程序通过该路径查找图像,但在发布模式下,它会跳过文件夹 Content/ 并尝试查找 images/

  body {
    margin: 0 auto;
    font-family: Arial;
    font-size: 12px;
    background: url(images/main_bg.jpg) repeat center top fixed;
    background-size: 100%;
    vertical-align: top;
    font-weight: normal;
  }

这就是我在 CSS 中使用图像的方式

bundles.Add(
            new StyleBundle("~/layout-styleNew")
                .Include("~/Content/bootstrap/css/*.css")
                .Include("~/Content/bootstrap/flatly/*.css")
                .Include("~/Content/helpful.css")
                .Include("~/Content/styleNew.css")
                .Include("~/Content/css/ToastMsg/css/*.css")
                .Include("~/Content/css/jquery-ui/*.css"));

<head>
   <meta charset="utf-8" />
   <meta name="viewport" content="width=device-width" />
   <title>@this.ViewBag.Title</title>

   @Scripts.Render("~/layout")
   @Styles.Render("~/layout-styleNew")
   @RenderSection("head", false)

</head>

【问题讨论】:

  • 请包含应该包含这些资源的代码..
  • 或者给我们演示/站点的链接,我们可以在这里检查这个问题。
  • @Leothelion,目前没有演示站点存在:(
  • 没有。不是资源:您如何将它们包含在 HTML 输出中(因此您的 WebForm/Razor/Html 代码)。
  • @Richard,我将此 css 文件添加到包中并将其包含在 _Layout.cshtml 中

标签: css asp.net-mvc bundle


【解决方案1】:

你的问题是真实路径。

在你的 CSS 中你有:

background: url(images/main_bg.jpg) repeat center top fixed;

这意味着客户端正在与 CSS 相同的文件夹中查找名为“images”的文件夹。在调试中,这是找到 CSS 文件的 Content 文件夹的子文件夹。

但是当你捆绑时,客户端会看到根文件夹中的 CSS,因此会创建一个相对于根目录的路径。

您需要为您的包指定一个与真实路径足够相似的名称,以便正确解析回溯路径。也许是“内容/AllStyles”?

【讨论】:

  • 我试过这个但不幸...客户再次这样做:GET http://localhost:4715/images/main_bg.jpg 404 (Not Found)。我已经用下一种方式改变了我的包:bundles.Add( new StyleBundle("/Content/layout-styleNew") .Include(...)我尝试了很多带有“/”和没有的“修改”
  • 我应该将图像包含到 Bundle 中还是没有必要?
  • @demo 捆绑图像需要改变一些东西,以便客户端知道在哪里使用合成图像的哪个位(参见“CSS Sprites”)。所以不:不要捆绑图片。
  • @demo 要调试它,您需要查看客户端用于捆绑的 URL,从该 URL 和相对路径中计算出图像的路径并进行调整,直到他们对齐。您浏览器的开发工具的网络选项卡将非常有用。
  • 这帮助我解决了我的问题。从浏览器调试器查看请求的路径 url(images/image.gif) 时。您可以看到对 content/images 的请求 404,其中 content 是我的包的名称(例如 ~/Content/cssfile.css),但实际位置是 content/somedirectory/images。您需要做的是将您的包更改为 ~/Content/somedirectory/cssfile.css 以便它正确映射到 content/somedirectory/images/image.gif
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-11
  • 1970-01-01
  • 1970-01-01
  • 2020-01-05
  • 1970-01-01
相关资源
最近更新 更多