0

If have some HTML which is stored in the Database. I am rendering the HTML using the below code in my view File.

@Html.Raw(Model.Content.ToString())

This is working fine except one problem, the image paths are not getting rendered properly. In my HTML(which is stored in the database) the image is defined as:

<img src="~/images/logo.png" />

But while is is rendered in the HTML it is attaching the URL of the website before the path due to which the images are not being displayed.

http://www.hostname.com/~/images/logo.png

Please comment in case any other information is required. Thanks for your time.

2
  • How are you injecting the images in the HTML and to the db? Commented Mar 4, 2015 at 14:29
  • From the FCK Editor I am storing the HTML to the database and then rendering it in the View. Commented Mar 4, 2015 at 14:44

1 Answer 1

2

It's normally Razor which interprets the "~/images/" and translates this into a relative path. Because you are rendering the HTML yourself (in Model.Content.ToString()) then you are skipping this feature.

If you need to store the HTML in the database, you probably should not use the ~/images/ syntax. If you will always publish your application at the root of a site, then just use /images/ instead (e.g. www.mysite.com/images rather than www.mysite.com/myapp/images)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this worked. Is there a better way to handle this? I am storing the HTML from the FCK Editor to the database and then rendering it in the View.
If your requirement is to store HTML in the database, then there is no easy alternative. If you don't actually need more functionality, stick with the simple solution!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.