图片未在MVC中显示 [英] Images Not Showing in MVC

查看:54
本文介绍了图片未在MVC中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事MVC4项目并遇到问题.我正在从事的项目是通过Web表单开发的.页面的html已保存在Database中.我正在从DB获取此html并将其显示在Views上.现在的问题是,此HTML aslo包含图像和链接,它们在Web窗体应用程序中工作正常,但是在MVC图像和链接中却无法正常工作,即,尽管我使用相同的文件夹结构,但由于无效的路径而无法加载图像.这是一个例子.

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.

假设我在App_Themes/Theme1/Img/download_icon.gif文件夹中有一个图像.现在在MVC项目中,如果我给此图像提供像/App_Themes/Theme1/Img/download_icon.gif这样的src,它可以正确加载,但是如果我给它给其像App_Themes/Theme1/Img/download_icon.gif这样的src,它就不会加载,这是保存在Db中.我的问题是如何正确加载html中来自Db的所有图像?

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?

推荐答案

您遇到了一个棘手的情况.将Html标签保留在数据库中始终是一个坏习惯,因为将来可能会导致此类问题,这给当前开发人员带来了很多麻烦.根据我的观点,没有解决方案,但我建议您解决此问题.这是虚拟目录的问题,希望我们可以被 Url.content 所钟爱.

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.

无论如何,您都是从数据库获取html的.因此,我相信您有与此类似的东西. string htmlContent = getHtmlforPage1()它将返回第一页的所有html,并使用 HtmlString()在视图中显示它.

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.

您可以将< img src = blah \ blah.img/> 替换为< img src =" + Url.Content(blah \ blah.img)/> .只需编写一个函数即可实现此目的.

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.

此问题将为您提供有关url.content()的需求为什么使用@ Url.Content

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

如果可能,请进行更改,避免将html存储在数据库中

If possible please change avoid storing html in database

这篇关于图片未在MVC中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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