如何解决根相对URL为背景图像的样式属性 [英] How to resolve root relative URL in a style attribute for background-image

查看:144
本文介绍了如何解决根相对URL为背景图像的样式属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何配置风格背景图片的URL到图像的绝对路径

 < D​​IV的风格=背景图像:网址('~/images/department/@Model.DepartmentImage');宽度:128像素,高度:128像素>
    < D​​IV> @ Html.Raw(Model.ImageMap)LT; / DIV>
< / DIV>


解决方案

在外部样式表一个单独的类是清洁剂(和路径将是相对于样式表)。

不过,您可以使用 Url.Content(〜/图片/部门/+ Model.DepartmentImage)内部视图返回完整的,解决路径。

请注意,这不是一个绝对的URL(详细信息),但基于你比如语法,你正在寻找的URL解析为应用程序的根,无论在哪里在目录层次结构中的URL输出。

How do I configure the style background-image url to the absolute path of the image

<div style="background-image:url('~/images/department/@Model.DepartmentImage'); width:128px; height:128px">
    <div>@Html.Raw(Model.ImageMap)</div>
</div>

解决方案

A separate class in an external stylesheet is cleaner (and the path will be relative to the stylesheet).

However, you can use Url.Content('~/images/department/" + Model.DepartmentImage ) inside your view to return the complete, resolved path.

Note that this isn't an absolute URL (details), but based on your example syntax you are looking for the URL to be resolved to the application root, regardless of where in the directory hierarchy the URL is output.

这篇关于如何解决根相对URL为背景图像的样式属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆