0

I am working on MVC4 project and facing a problem.The project on which i am working was developed in web forms.The html of pages is already saved in Database.I am getting this html from DB and showing it on Views. Now the problem is that this Html aslo contains Images and links and these are working fine in web forms application but in MVC images and links are not working i.e images fail to load due to invalid path although i have made the same folder structure. Here is an example.

Suppose i have an image in folder App_Themes/Theme1/Img/download_icon.gif. Now in MVC project if i give this image the src like /App_Themes/Theme1/Img/download_icon.gif it is loaded correctly but if i give it the src like App_Themes/Theme1/Img/download_icon.gif it does not load and this is saved in Db. My question is how can i load all images correctly that come from Db in html?

Mairaj Ahmad
  • 14,434
  • 2
  • 26
  • 40

1 Answers1

0

You got a tricky scenario. Its always a bad practice to keep Html tags in database because it may cause these kind of issues in future, which gives head aches to current developer working on that. As per my opinion there is no solution, but I am suggesting a work around for you to overcome this. This is an issue with virtual directory and hope that we can resloved by Url.content.

Any way your are getting the html from database. So I believe that you have something similar to this. string htmlContent = getHtmlforPage1() it will returns you all the html for page one and you use HtmlString() to show it in your view.

You can replace the <img src=blah\blah.img /> with "<img src="+ Url.Content(blah\blah.img") />. all you need is to write down a function to achieve this.

This question will gives you about the need for url.content() why use @Url.Content

If possible please change avoid storing html in database

Community
  • 1
  • 1
kbvishnu
  • 14,760
  • 19
  • 71
  • 101
  • 1
    Well i know about Url.Content and that's exactly what i want to do, but i hardcoded an image with Url.Content and it was not displayed i wrote this if i write it like then it is displayed – Mairaj Ahmad Aug 17 '13 at 08:42
  • Yes that is. @Url.Content is used for Razor. But we need to replace with the value in controller, so you need to use Url.Content – kbvishnu Aug 17 '13 at 08:51
  • Is there anything like An ActionAttribute by which i can get image path and resolve it. – Mairaj Ahmad Aug 17 '13 at 09:33
  • Action attributes is like some thing which need to trigger before or after executing an action.it can be for authorization etc. I think you cannot do it. If possible share the code – kbvishnu Aug 17 '13 at 09:38