如何显示在 Asp.net MVC 中找不到的图像
我有一个 ASP.NET MVC 项目。在特定视图中,我将来自项目内不同文件夹的四个图像称为。
有时找不到图像。我想为每个文件夹设置一个图像,因为所有 4 个文件夹都包含不同大小的图像,所以我需要为每个文件夹设置特定大小的图像。
当图像找不到任何方法时,我如何显示默认图像。我的意思是,当目录中没有我在视图中调用的图像时,调用 none.png 。
在找不到图像的情况下他们有什么方式显示图像。
在 asp.net 4 MVC 3. 中使用 web.config 或设置其他任何内容可以实现此目的。
I have a asp.net mvc project. in a speicific views i call the four image from different different folder inside my proect.
sometime image can not be found. i want to set a image for every folder because all 4 folder contain different diffent size of image so i need to set the image of specific size for each folder.
how i can show the default image when the image not found any way to do that.i means call the none.png when the directory not have image who i called in the views.
are their any way to show the image in the case of image not found.
any way to do that in asp.net 4 MVC 3. using web.config or setting anything else.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使控制器操作返回
FileResult
,并接受描述图像路径的string
类型的参数。控制器操作尝试从磁盘加载图像并将其作为
FileResult
返回,或者如果磁盘中缺少该文件,则返回占位符“未找到图像”图像。然后,要显示图像(或其占位符),您需要将
img
元素的src
属性设置为控制器操作,而不是直接设置图像。You can make a controller action return a
FileResult
, and accept a parameter of typestring
which describes the image path.The controller action tries to load the image from disk and return it as a
FileResult
, or if the file is missing from the disk, return a place holder 'image not found' image instead.Then to display the image (or its placeholder) you would set the
src
attribute of yourimg
element to the controller action rather than the image directly.最简单的方法是使用 html 助手。请注意在显示图像文件名之前检查文件系统会对性能造成额外影响。不过,点击量很小,因此除非流量非常高,否则您不会注意到任何问题。然后,您可以实现某种缓存,以便应用程序“知道”文件是否存在。
您可以为其使用自定义 html 帮助程序
然后在您看来,您可以使用以下命令创建 url:
编辑: 正如 Jim 指出的那样,我还没有真正解决大小调整问题。就我个人而言,我使用自动大小请求管理/大小,这是一个完全不同的故事,但如果您担心文件夹/大小,只需将该信息传递到构建路径即可。如下所示:
然后在您看来,您可以使用以下方式创建网址:
如果文件夹是固定集,建议使用枚举来更好地进行编程控制,但为了一种快速而令人讨厌的方法,而不是您不能只使用字符串的原因如果该文件夹是数据库生成的等。
Easiest way to do this is with a html helper. Please be mindful of the extra performace hit of checking the file system before even showing an image filename. The hit is a small one though, so you won't notice any issues unless you are getting very high traffic. Then you can implement some sort of caching so the app "knows" if the file exists or not.
You can use a custom html helper for it
Then in your view you can just make the url using:
EDIT: As Jim pointed out, I haven't really addressed the sizing issue. Personally I use automatic size request management/size which is a whole other story, but if you are concerned about the folders/sizes simply pass that information in to build the path. As below:
Then in your view you can just make the url using:
Have suggested an Enum for better programmatic control if the folder is a fixed set, but for a quick and nasty approach, not reason why you can't just use a string if the folder is db generated etc.
我不太了解 MVC,但我的想法仍然是:
有一个特定的控制器,它可以根据您的逻辑获取图像。就像迈克尔说的那样,在成功审核后返回一些东西(图像) - 意思是如果找到图像。如果未找到,则返回状态代码 404 或类似的内容。
通过客户端上的 javascript 处理其余部分。所以在img标签中为onerror事件写一些东西。可能会用找不到海报图像文件的图像替换 img 源。这里有一些(但不完全)如何通过 jquery 做到这一点。
PS:http://jsfiddle.net/Fy9SL/2/ 如果您对完整演示感兴趣jquery 代码。
Don't know all that much MVC, but here is my idea nonetheless:
Have a specific controller which fetches an image based on whatever your logic. Like Michael said have something (image) returned on a successful audit - meaning if image is found. If not found return a status code 404 or something like that.
Handle the rest via javascript on the client. So in the img tag write something for the onerror event. May be replace the img source w/ your image not found poster image file. Here somewhat (but not exactly) how to do that via jquery.
PS: http://jsfiddle.net/Fy9SL/2/ if u are interested in the full demo of jquery code.