Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Url.Content with semi-relative URL

I am storing the location of images in my database in an MVC application...but i am only storing part of the location. For example:

/headers/image1.jpg
/headers/image2.jpg

The images are actually stored in the following folder:

~/content/images/headers/image1.jpg
~/content/images/headers/image1.jpg

In my view, I want to do something like this:

<img src="@Url.Content("~/content/images") + Model.ImageUrl" />

How can I do this?

like image 908
Dismissile Avatar asked Mar 16 '11 21:03

Dismissile


1 Answers

Just do it!

<img src="@Url.Content("~/content/images" + Model.ImageUrl)" />

UPDATE:

As of ASP.NET MVC 4 it is valid to use the tilde URLs directly in HTML as the Razor View Engine will parse the URLs. Like this:

<img src="~/content/images/@Model.ImageUrl" />
like image 59
Mikael Östberg Avatar answered Nov 18 '22 04:11

Mikael Östberg