1

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>
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
ONYX
  • 5,679
  • 15
  • 83
  • 146

1 Answers1

1

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.

Community
  • 1
  • 1
Tim M.
  • 53,671
  • 14
  • 120
  • 163
  • I just did a test, and Razor (latest) will not resolve the URL found in `style="background-image:url('~/abc')"` – Tim M. Sep 08 '13 at 23:32
  • 1
    the code I provided doesn't resolve the url in the page it just shows ~/images/departments/blah.png but it now works thanks to you cheers – ONYX Sep 08 '13 at 23:33