c# - <httpErrors> 不显示 css 或图像

标签 c# css asp.net .net web-config

在我的 web.config 中,我正在尝试处理服务器错误,我已将这段代码放在 <system.webServer> 中:

<httpErrors errorMode="Custom" defaultResponseMode="File">
      <remove statusCode="404"/>
      <error statusCode="404" path="FileNotFoundError.html" />
      <remove statusCode="403"/>
      <error statusCode="403" path="NoAccessErrorPage.html" />
</httpErrors>

当我尝试访问 http://localhost:12112/asdd 时其中 asdd 指的是任何目录或文件。我得到了具有适当样式和图像的所需页面。然而,当我点击 http://localhost:12810/asdd/asdd一个不存在的目录中的额外目录,我不再获得我的 css 或图像。

这是我的图像定义src="Images/Image1.png" 我在头部的 CSS 定义:<link rel="stylesheet" type="text/css" href="style.css" />

注意:如果我使用内联样式,我可以获得我的 css,但我没有办法处理图像,如果可能的话,我想使用外部样式表。

最佳答案

您应该使用 ~ 运算符 ( msdn ):

src="~/Images/Image1.png"

ASP.NET includes the Web application root operator (~), which you can use when specifying a path in server controls. ASP.NET resolves the ~ operator to the root of the current application. You can use the ~ operator in conjunction with folders to specify a path that is based on the current root.

关于c# - <httpErrors> 不显示 css 或图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46591325/

相关文章:

c# - 编写一个除了抛出异常什么都不做的方法是不好的做法吗?

c# - LabelFor不显示值?

html - 结合两个 CSS 网格的布局

javascript - JSFIDDLE 无法正常工作

c# - ASP.NET MVC 6 Controller 工厂

c# - 从其子类初始化 C# Tuple<T, T> 以便在 WPF 中使用

c# - 在滚动事件处理程序上多次触发事件

jquery - Accordion 图标不出现

javascript - 为什么我的灯箱画廊会在单独的页面中加载 img?

c# - 使用 javascript 获取 Windows 用户名