css - 与布局中的不同 View 共享背景图像

标签 css asp.net-mvc razor

我有一个带有背景图像的简单自定义布局 (PaintLayout.cshtml):

<head>
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    <link href="/Content/PaintStyles.css" rel="stylesheet" type="text/css" /> 
</head> 
<body style="background: url('Images/PaintBackgroundSM.png')">
    <div id="header">
        <h1>@ViewBag.Headline</h1>
    </div>

    <div>
        @RenderBody()
    </div> 
</body>

Index.cshtml 显示图像很好

@{
Layout = "~/Views/Shared/PaintLayout.cshtml";
}

<div style="text-align:center">
    <h1>@ViewBag.Message</h1>
</div>

但是 View 没有。但是,它确实从 PaintStyles.css 获取其余的 css,而不是布局中的图像

@{
Layout = "~/Views/Shared/PaintLayout.cshtml";
}

<div>
    <h2>ShowDetail</h2>
</div>

我在图像路径上尝试了几种变体。我使用的是唯一一个甚至可以在 Index.cshtml 上运行的。我的图像文件夹位于根目录中,模板放置它的位置。我的索引和 View 都在 Views\Paint 下的相同路径中。

最佳答案

您需要使用可以在您的应用程序中的任何位置解析的路径:

或者(更简单、更清晰)将路径放入您的样式表中,它将被解析 relative to the stylesheet .

body{
    /* use a path relative to the stylesheet's location */
    background: url('../Images/PaintBackgroundSM.png')
}

关于css - 与布局中的不同 View 共享背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22261204/

相关文章:

html - CSS:图形居中,然后向右继续一个模式

css - Chrome 的表格单元格填充问题

html - div 不限制自己在父级内

asp.net-mvc - ASP.NET MVC 3.0 -- 执行处理程序 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper' 的子请求时出错

asp.net-mvc - ASP.net MVC 4 WebApi 中的嵌套资源

html - zurb foundation 将一行分成两列

sql-server - SQL Server 2008 R2 缺少参数

Jquery Datepicker 在 Visual Studio 中工作,但在部署时不起作用

asp.net-mvc - 使用 HTML Action 链接指向一个 div

javascript - 在 razor foreach 语句中调用 JavaScript 函数