【问题标题】:Error page in ASP MVC [duplicate]ASP MVC 中的错误页面 [重复]
【发布时间】:2017-04-05 02:59:23
【问题描述】:

我的 404.html 包含:

<img class="img-responsive center-block" src="Images/error404.png"/>

我的项目文件夹中的 Web.config 包含:

<httpErrors errorMode="Custom" existingResponse="Replace">
     <remove statusCode="404"/>
     <error statusCode="404" path="404.html" responseMode="File"/>
</httpErrors>

我可以在浏览器中通过http://localhost:53870/Images/error404.png访问图片。

当我直接调用 404.html 中的图像时正在加载它

localhost:53870/404.html

或者当我打电话时

http://localhost:53870/NonexistentController

当我调用这样的东西时,404.html 中的图像无法加载

http://localhost:53870/Home/NonexistentAction

【问题讨论】:

  • src前面加一个斜线:&lt;img class="img-responsive center-block" src="/Images/error404.png"/&gt;
  • @Liam 谢谢!!)

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


【解决方案1】:

您在图像路径的开头缺少反斜杠。

变化:

<img class="img-responsive center-block" src="Images/error404.png"/>

收件人:

<img class="img-responsive center-block" src="/Images/error404.png"/>

【讨论】:

    【解决方案2】:

    在 razor 文件中,您可以利用 ~ 运算符。~ 将返回虚拟根路径。

    &lt;img class="img-responsive center-block" src="~/Images/error404.png"/&gt;

    这样,无论您在哪个子文件夹/路径中,您的图像 src 值都是正确的。

    【讨论】:

    • 我一直不明白这一点。扁平化的 HTML 以 / 开头。为什么微软也添加了这个我永远不会知道
    猜你喜欢
    • 2015-07-28
    • 1970-01-01
    • 2016-06-21
    • 2013-09-15
    • 1970-01-01
    • 2012-05-29
    • 2016-05-31
    • 2014-07-27
    • 1970-01-01
    相关资源
    最近更新 更多